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 / test_worker.py View on Github external
def make_channel_layer(self, name):
        """Make wrapped in memory channel layer for test purposes."""

        return ChannelLayerWrapper(
            channel_layer=InMemoryChannelLayer(),
            alias=name,
            routing=[],
        )
github django / channels / tests / test_management.py View on Github external
import logging

from asgiref.inmemory import ChannelLayer
from django.core.management 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 channels.management.commands import runserver
from channels.staticfiles import StaticFilesConsumer


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


@mock.patch('channels.management.commands.runworker.Worker')
class RunWorkerTests(TestCase):

    def setUp(self):
        import channels.log
        self.stream = StringIO()
        channels.log.handler = logging.StreamHandler(self.stream)
        BindingMetaclass.binding_classes = []
        self._old_layer = channel_layers.set(
            'fake_channel',
github django / asgiref / asgiref / inmemory.py 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 / inmemory.py View on Github external
def __init__(self, expiry=60, group_expiry=86400, capacity=10, channel_capacity=None):
        super(ChannelLayer, self).__init__(
            expiry=expiry,
            group_expiry=group_expiry,
            capacity=capacity,
            channel_capacity=channel_capacity,
        )
        self.thread_lock = threading.Lock()
        # Storage for state
        self._channels = {}
        self._groups = {}