How to use the asgiref.inmemory.ChannelLayer function in asgiref

To help you get started, we’ve selected a few asgiref examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github django / channels / tests / View on Github external
def make_channel_layer(self, name):
        """Make wrapped in memory channel layer for test purposes."""

        return ChannelLayerWrapper(
github django / channels / tests / View on Github external
import logging

from asgiref.inmemory import ChannelLayer
from import CommandError, call_command
from django.test import TestCase, mock
from six import StringIO

from channels.asgi import channel_layers, ChannelLayerWrapper
from channels.binding.base import BindingMetaclass
from channels.handler import ViewConsumer
from import runserver
from channels.staticfiles import StaticFilesConsumer

class FakeChannelLayer(ChannelLayer):
    Dummy class to bypass the 'inmemory' string check.

class RunWorkerTests(TestCase):

    def setUp(self):
        import channels.log = StringIO()
        channels.log.handler = logging.StreamHandler(
        BindingMetaclass.binding_classes = []
        self._old_layer = channel_layers.set(
github django / asgiref / asgiref / View on Github external
for channel, added in list(channels.items()):
                    if added < (time.time() - self.group_expiry):
                        del self._groups[group][channel]
                        if not self._groups[group]:
                            del self._groups[group]

    def _remove_from_groups(self, channel):
        Removes a channel from all groups. Used when a message on it expires.
        for channels in self._groups.values():
            if channel in channels:
                del channels[channel]

# Global single instance for easy use
channel_layer = ChannelLayer()
github django / asgiref / asgiref / View on Github external
def __init__(self, expiry=60, group_expiry=86400, capacity=10, channel_capacity=None):
        super(ChannelLayer, self).__init__(
        self.thread_lock = threading.Lock()
        # Storage for state
        self._channels = {}
        self._groups = {}