How to use the channels.db.database_sync_to_async function in channels

To help you get started, we’ve selected a few channels 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 hishnash / djangochannelsrestframework / tests / test_model_observer.py View on Github external
)

    response = await communicator.receive_json_from()

    assert response == {
        "action": "retrieve",
        "errors": ["Not found"],
        "response_status": 404,
        "request_id": 1,
        "data": None
    }

    u1 = await database_sync_to_async(get_user_model().objects.create)(
        username='test1', email='42@example.com'
    )
    u2 = await database_sync_to_async(get_user_model().objects.create)(
        username='test2', email='45@example.com'
    )

    # lookup a pk that is not there
    await communicator.send_json_to(
        {
            "action": "retrieve",
            "pk": u1.id - 1,
            "request_id": 1
        }
    )

    response = await communicator.receive_json_from()

    assert response == {
        "action": "retrieve",
github genialis / django-rest-framework-reactive / src / rest_framework_reactive / consumers.py View on Github external
        @database_sync_to_async
        def get_observers(table):
            # Find all observers with dependencies on the given table.
            return list(
                Observer.objects.filter(
                    dependencies__table=table, subscribers__isnull=False
                )
                .distinct('pk')
                .values_list('pk', flat=True)
            )
github django / channels / channels / sessions.py View on Github external
async def __call__(self, receive, send):
        """
        We intercept the send() callable so we can do session saves and
        add session cookie overrides to send back.
        """
        # Resolve the session now we can do it in a blocking way
        session_key = self.scope["cookies"].get(self.middleware.cookie_name)
        self.scope["session"]._wrapped = await database_sync_to_async(
            self.middleware.session_store
        )(session_key)
        # Override send
        self.real_send = send
        return await self.inner(receive, self.send)
github pujanm / Chat-IO / chat / consumers.py View on Github external
    @database_sync_to_async
    def get_users(self, id):
        id = int(id)
        c = ChatRoom.objects.filter(id=id)[0]
        l = [c.sender.username, c.receiver.username]

        return l
github Murrengan / murr / murr_chat / consumers / chat.py View on Github external
    @database_sync_to_async
    def remove_chat_member(self, user_id):
        user = get_user_model().objects.filter(id=user_id).first()
        log = ''
        if user:
            answer = MurrChatMembers.objects.filter(user=user_id).delete()[0]
            if answer:
                log = 'Муррен удален из группы'
            else:
                log = 'Ошибка удаления или 0'
        return log
github TyVik / geopuzzle / common / consumer.py View on Github external
    @database_sync_to_async
    def get_object(self, pk: int):
        raise NotImplementedError()
github hishnash / djangochannelsrestframework / djangochannelsrestframework / observer / generics.py View on Github external
async def subscribe_instance(self, request_id=None, **kwargs):
        if request_id is None:
            raise ValueError("request_id must have a value set")
        # subscribe!
        instance = await database_sync_to_async(self.get_object)(**kwargs)
        await self.handle_instance_change.subscribe(instance=instance)
        self.subscribed_requests[self.__class__.handle_instance_change] = request_id

        return None, status.HTTP_201_CREATED
github codingforentrepreneurs / ChatXChannels / src / chat / consumers.py View on Github external
    @database_sync_to_async
    def create_welcome_chat_message(self, thread, user, message):
        return ChatMessage.objects.create(thread=thread, user=user, message=message)
github ModelChimp / modelchimp / modelchimp / views / consumers / tracker.py View on Github external
    @database_sync_to_async
    def register_experiment(self, experiment_id):
        '''
        Check if the experiment already exists and if not then add the experiment to
        the project
        '''
        try:
            experiment = Experiment.objects.get(experiment_id=experiment_id)
        except Experiment.DoesNotExist:
            experiment = Experiment.objects.create(experiment_id=experiment_id,
                project = self.project,
                user = self.user,
                name = experiment_id,
                status = ExperimentStatus.IN_PROCESS
             )

        return experiment