Trying to do so causes threading errors:
2018-02-03 06:43:49,209 - ERROR - server - Exception inside application: There is no current event loop in thread 'Thread-4'.
File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File "/mnt/c/Users/Andrew/Programs/channels/channels/consumer.py", line 51, in __call__
await await_many_dispatch([receive, self.channel_receive], self.dispatch)
File "/mnt/c/Users/Andrew/Programs/channels/channels/utils.py", line 48, in await_many_dispatch
await dispatch(result)
File "/home/andrew/Envs/django/lib/python3.5/site-packages/asgiref/sync.py", line 81, in inner
return await async_func(*args, **kwargs)
File "/home/andrew/Envs/django/lib/python3.5/site-packages/asgiref/sync.py", line 65, in __call__
return await asyncio.wait_for(future, timeout=None)
File "/usr/lib/python3.5/asyncio/tasks.py", line 373, in wait_for
return (yield from fut)
File "/usr/lib/python3.5/asyncio/futures.py", line 361, in __iter__
yield self # This tells Task to wait for completion.
File "/usr/lib/python3.5/asyncio/tasks.py", line 296, in _wakeup
future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/andrew/Envs/django/lib/python3.5/site-packages/asgiref/sync.py", line 74, in thread_handler
raise e
File "/home/andrew/Envs/django/lib/python3.5/site-packages/asgiref/sync.py", line 72, in thread_handler
self.func(*args, **kwargs)
File "/mnt/c/Users/Andrew/Programs/channels/channels/consumer.py", line 93, in dispatch
handler(message)
File "/mnt/c/Users/Andrew/Programs/channels/channels/generic/websocket.py", line 19, in websocket_connect
self.connect()
File "/mnt/c/Users/Andrew/Programs/django_test/messaging/consumers.py", line 12, in connect
AsyncToSync(self.channel_layer.group_add)("chat", self.channel_name)
File "/home/andrew/Envs/django/lib/python3.5/site-packages/asgiref/sync.py", line 17, in __init__
self.main_event_loop = asyncio.get_event_loop()
File "/usr/lib/python3.5/asyncio/events.py", line 632, in get_event_loop
return get_event_loop_policy().get_event_loop()
File "/usr/lib/python3.5/asyncio/events.py", line 578, in get_event_loop
% threading.current_thread().name)
There is no current event loop in thread 'Thread-4'.
This is because AsyncToSync is designed to be bound to functions in the main thread, not inside a subthread, so the documentation is wrong. It needs either a differently-documented approach, or to be fixed so it can find its main thread.
Solved in django/asgiref@9223f0be259ecfb234d344269e9b61b9126b37f4- SyncToAsync now stores the async event loop in a threadlocal so you can then call AsyncToSync inside threads it makes. Released in asgiref 2.1.1.
Most helpful comment
Solved in django/asgiref@9223f0be259ecfb234d344269e9b61b9126b37f4- SyncToAsync now stores the async event loop in a threadlocal so you can then call AsyncToSync inside threads it makes. Released in
asgiref 2.1.1.