question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using uvicorn + gunicorn as ASGI server fails to boot

See original GitHub issue

With latest update of twisted 21.2.0 the app stops to inicialize when using gunicorn + uvicorn as ASGI server.

  • Your OS and runtime environment, and browser if applicable Using docker image python:3.9.2-slim-buster

  • A pip freeze output showing your package versions

aioredis==1.3.1
appnope==0.1.2
asgiref==3.3.1
async-timeout==3.0.1
attrs==20.3.0
autobahn==21.2.2
Automat==20.2.0
backcall==0.2.0
cffi==1.14.5
channels==3.0.3
channels-redis==3.2.0
click==7.1.2
constantly==15.1.0
coverage==5.4
cryptography==3.4.6
daphne==3.0.1
decorator==4.4.2
Django==3.1.7
django-cors-headers==3.7.0
django-redis==4.12.1
djangorestframework==3.12.2
drf-extensions==0.7.0
factory-boy==3.2.0
Faker==6.5.0
freezegun==1.1.0
gunicorn==20.0.4
h11==0.12.0
hiredis==1.1.0
httptools==0.1.1
hyperlink==21.0.0
idna==3.1
incremental==17.5.0
iniconfig==1.1.1
ipdb==0.13.4
ipython==7.21.0
ipython-genutils==0.2.0
jedi==0.18.0
msgpack==1.0.2
munch==2.5.0
packaging==20.9
parso==0.8.1
pexpect==4.8.0
pickleshare==0.7.5
pluggy==0.13.1
prompt-toolkit==3.0.16
psycopg2==2.8.6
ptyprocess==0.7.0
py==1.10.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.20
Pygments==2.8.0
pyOpenSSL==20.0.1
pyparsing==2.4.7
pytest==6.2.2
pytest-cov==2.11.1
pytest-django==4.1.0
pytest-freezegun==0.4.2
python-dateutil==2.8.1
python-dotenv==0.15.0
pytz==2021.1
PyYAML==5.4.1
redis==3.5.3
service-identity==18.1.0
six==1.15.0
sqlparse==0.4.1
text-unidecode==1.3
toml==0.10.2
traitlets==5.0.5
Twisted==21.2.0
txaio==21.2.1
uvicorn==0.13.4
uvloop==0.15.2
watchgod==0.7
wcwidth==0.2.5
websockets==8.1
zope.interface==5.2.0
  • What you expected to happen vs. what actually happened I expect to server boot up.

  • How you’re running Channels (runserver? daphne/runworker? Nginx/Apache in front?) gunicorn -w 4 -b 0.0.0.0:8000 -k uvicorn.workers.UvicornWorker asgi:application

  • Console logs and full tracebacks of any errors

[2021-03-01 12:48:40 +0000] [7] [INFO] Worker exiting (pid: 7)
[2021-03-01 12:48:40 +0000] [7] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
worker.init_process()
File "/usr/local/lib/python3.9/site-packages/uvicorn/workers.py", line 63, in init_process
super(UvicornWorker, self).init_process()
File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 119, in init_process
self.load_wsgi()
File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/local/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
return self.load_wsgiapp()
File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/local/lib/python3.9/site-packages/gunicorn/util.py", line 358, in import_app
mod = importlib.import_module(module)
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/app/asgi.py", line 17, in <module>
django_asgi_application = get_asgi_application()
File "/usr/local/lib/python3.9/site-packages/django/core/asgi.py", line 12, in get_asgi_application
django.setup(set_prefix=False)
File "/usr/local/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.9/site-packages/ddtrace/contrib/trace_utils.py", line 48, in wrapper
return func(mod, pin, wrapped, instance, args, kwargs)
File "/usr/local/lib/python3.9/site-packages/ddtrace/contrib/django/patch.py", line 166, in traced_populate
ret = func(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/usr/local/lib/python3.9/site-packages/django/apps/config.py", line 116, in create
mod = import_module(mod_path)
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/usr/local/lib/python3.9/site-packages/channels/apps.py", line 4, in <module>
import daphne.server
File "/usr/local/lib/python3.9/site-packages/daphne/server.py", line 20, in <module>
asyncioreactor.install(twisted_loop)
File "/usr/local/lib/python3.9/site-packages/twisted/internet/asyncioreactor.py", line 307, in install
reactor = AsyncioSelectorReactor(eventloop)
File "/usr/local/lib/python3.9/site-packages/twisted/internet/asyncioreactor.py", line 60, in __init__
raise TypeError(
TypeError: SelectorEventLoop required, instead got: <uvloop.Loop running=False closed=False debug=False>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:17
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
bertonhacommented, Apr 12, 2021

@Uninen just set twisted on <21

But your asgi.py has a small issue you need to instanciate django app before use the AuthMiddlewareStack(…)

As on the docs https://channels.readthedocs.io/en/stable/deploying.html#configuring-the-asgi-application

2reactions
littlebtccommented, Jul 29, 2021

FYI, Twisted 21.7.0 released with fixes for this issue:

  • twisted.internet.asyncioreactor.AsyncioSelectorReactor will no longer raise a TypeError like “SelectorEventLoop required, instead got: <uvloop.Loop …>” (broken since 21.2.0). (#10106)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using uvicorn + gunicorn as ASGI server fails to boot
With latest update of twisted 21.2.0 the app stops to inicialize when using gunicorn + uvicorn as ASGI server.
Read more >
Deployment - Uvicorn
Typically you'll run uvicorn from the command line. ... The ASGI application should be specified in the form path.to.module:instance.path . When running locally, ......
Read more >
How To Set Up an ASGI Django App with Postgres, Nginx ...
To use uvicorn workers with the gunicorn server, enter your project directory and use the following gunicorn command to load the project's ASGI...
Read more >
<HaltServer 'Worker failed to boot.' 3> django - Stack Overflow
run guncorn with --preload can see the error log, like this gunicorn app:application --preload -b 0.0.0.0:5000. This will usually give you a more...
Read more >
Moving from wsgi gunicorn to asgi gunicorn with uvicorn ...
I did notice one thing with `unvicorn` -- but don't know if it's related: During the startup of our application, it gives the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found