Avoid Twisted using the default event loop

When switching threads, e.g. when run via Django auto-reloader, the default run loop causes issues detecting async contexts.
Fixes https://github.com/django/channels/issues/1374
This commit is contained in:
Carlton Gibson 2019-12-18 20:05:50 +01:00 committed by Carlton Gibson
parent eb582d1d43
commit 27f760a814

View File

@ -1,8 +1,10 @@
# This has to be done first as Twisted is import-order-sensitive with reactors
import asyncio # isort:skip
import sys # isort:skip
import warnings # isort:skip
from twisted.internet import asyncioreactor # isort:skip
twisted_loop = asyncio.new_event_loop()
current_reactor = sys.modules.get("twisted.internet.reactor", None)
if current_reactor is not None:
if not isinstance(current_reactor, asyncioreactor.AsyncioSelectorReactor):
@ -13,11 +15,10 @@ if current_reactor is not None:
UserWarning,
)
del sys.modules["twisted.internet.reactor"]
asyncioreactor.install()
asyncioreactor.install(twisted_loop)
else:
asyncioreactor.install()
asyncioreactor.install(twisted_loop)
import asyncio
import logging
import time
import traceback