Cannot access scope["user"] object from an async consumer
See original GitHub issueTraceback:
2018-04-13 17:44:51,382 - ERROR - server - Exception inside application: You cannot use AsyncToSync in the same thread as an async event loop - just await the async function directly.
File "/tmp/channels-examples/multichat/channels/consumer.py", line 54, in __call__
await await_many_dispatch([receive, self.channel_receive], self.dispatch)
File "/tmp/channels-examples/multichat/channels/utils.py", line 50, in await_many_dispatch
await dispatch(result)
File "/tmp/channels-examples/multichat/channels/consumer.py", line 67, in dispatch
await handler(message)
File "/tmp/channels-examples/multichat/channels/generic/websocket.py", line 173, in websocket_connect
await self.connect()
File "/tmp/channels-examples/multichat/chat/consumers.py", line 26, in connect
if self.scope["user"].is_anonymous:
File "/tmp/channels-examples/multichat/env/lib/python3.6/site-packages/django/utils/functional.py", line 215, in inner
self._setup()
File "/tmp/channels-examples/multichat/env/lib/python3.6/site-packages/django/utils/functional.py", line 349, in _setup
self._wrapped = self._setupfunc()
File "/tmp/channels-examples/multichat/channels/auth.py", line 142, in <lambda>
scope["user"] = SimpleLazyObject(lambda: async_to_sync(get_user)(scope))
File "/tmp/channels-examples/multichat/env/lib/python3.6/site-packages/asgiref/sync.py", line 34, in __call__
"You cannot use AsyncToSync in the same thread as an async event loop - "
You cannot use AsyncToSync in the same thread as an async event loop - just await the async function directly.
Using AuthMiddleware: accessing the scope[“user”] object from an asynchronous consumer triggers this error.
This happens since #923 was merged (published in Channels v2.1.0). The traceback can be reproduced with the following steps:
- clone andrewgodwin/channels-example and install dependencies.
- create a superuser
- create a chatroom on the admin site
- try to connect to that chatroom
Issue Analytics
- State:
- Created 5 years ago
- Comments:15 (5 by maintainers)
Top Results From Across the Web
can't get the profile object firstname - django - Stack Overflow
sync_to_async takes a callable, not the result. so, I need to do this: ... await PublicChatConsumer.get_user_profile(self.scope["user"]) ...
Read more >Consumers — Channels 4.0.0 documentation
A consumer is a subclass of either channels.consumer.AsyncConsumer or channels.consumer.SyncConsumer . As these names suggest, one will expect you to write ...
Read more >Asynchronous file access (C#) - Microsoft Learn
Learn how to use the async feature to access files in C#. You can call into asynchronous methods without using callbacks or splitting...
Read more >Introduction to Django Channels - TestDriven.io
This tutorial shows how to use Django Channels to create a real-time application.
Read more >Asynchronous support - Django documentation
You will only get the benefits of a fully-asynchronous request stack if you have ... async for author in Author.objects.filter(name__startswith="A"): book ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
OK this is released in channels 2.1.1.
Just confirming that this is an error caused by the release of Channels 2.1, but it’s also correct to complain. I am investigating possible fixes now.
In the meantime, if this is happening in your own code, you can replace
scope["user"]
withawait channels.auth.get_user(scope)