添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I did deployed using gunicorn and django and I'm getting a lot of this:

Aug 28 16:22:17 systemd[1]: Reloading gunicorn daemon.
Aug 28 16:22:17 systemd[1]: Reloaded gunicorn daemon.
Aug 28 16:22:17 gunicorn[738]: [2018-08-28 16:22:17 -0500] [738] [INFO] Handling signal: hup
Aug 28 16:22:17 gunicorn[738]: [2018-08-28 16:22:17 -0500] [738] [INFO] Hang up: Master
Aug 28 16:22:18 gunicorn[738]: [2018-08-28 16:22:18 -0500] [1418] [INFO] Booting worker with pid: 1418
Aug 28 16:22:18 gunicorn[738]: [2018-08-28 16:22:18 -0500] [1417] [INFO] Booting worker with pid: 1417
Aug 28 16:22:18 gunicorn[738]: [2018-08-28 16:22:17 -0500] [1416] [INFO] Booting worker with pid: 1416
Aug 28 16:22:18 gunicorn[738]: [2018-08-28 16:22:18 -0500] [1419] [INFO] Booting worker with pid: 1419
Aug 28 16:22:18 gunicorn[738]: [2018-08-28 16:22:18 -0500] [1209] [INFO] Error while closing socket [Errno 9] Bad file descriptor
Aug 28 16:22:18 gunicorn[738]: /usr/local/lib/python3.6/dist-packages/daphne/server.py:12: UserWarning: Something has already installed a non-asyncio Twisted reactor. Attempting to uninstall it; you can fix this warning by importing daphne.server early in your codebase or finding the package that imports Twisted and importing it later on.
Aug 28 16:22:18 gunicorn[738]:   UserWarning,
Aug 28 16:22:18 gunicorn[738]: [2018-08-28 16:22:18 -0500] [1210] [INFO] Error while closing socket [Errno 9] Bad file descriptor
Aug 28 16:22:18 gunicorn[738]: [2018-08-28 16:22:18 -0500] [1210] [INFO] Worker exiting (pid: 1210)
Aug 28 16:22:18 gunicorn[738]: [2018-08-28 16:22:18 -0500] [1209] [INFO] Worker exiting (pid: 1209)
Aug 28 16:22:18 gunicorn[738]: [2018-08-28 16:22:18 -0500] [1211] [INFO] Error while closing socket [Errno 9] Bad file descriptor
Aug 28 16:22:18 gunicorn[738]: [2018-08-28 16:22:18 -0500] [1211] [INFO] Worker exiting (pid: 1211)
Aug 28 16:22:18 gunicorn[738]: [2018-08-28 16:22:18 -0500] [1212] [INFO] Error while closing socket [Errno 9] Bad file descriptor
Aug 28 16:22:18 gunicorn[738]: [2018-08-28 16:22:18 -0500] [1212] [INFO] Worker exiting (pid: 1212)

Also it takes a solid 10 to 20 seconds to respond for the first time. When I use the --preload flag the time goes down to 1-3 seconds but it does not work well. The same error happens with or without the preload flag.

I'm using v0.3.2

@tomchristie I'm not running daphne at all. That was a warning on django-channels and the issue is not related at all with daphne.

There is something weird when running using the preload flag with gunicorn.

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
PIDFile=/run/gunicorn/pid
User=deploy
Group=deploy
RuntimeDirectory=gunicorn
WorkingDirectory=/home/deploy/django/
ExecStart=/usr/local/bin/gunicorn app.asgi:application --worker-class uvicorn.workers.UvicornWorker --pid /run/gunicorn/pid --bind unix:/run/gunicorn/socket --workers 4 --preload
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

Removing the --preload flag works fine but takes up to 60 seconds to be available to respond requests:

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
PIDFile=/run/gunicorn/pid
User=deploy
Group=deploy
RuntimeDirectory=gunicorn
WorkingDirectory=/home/deploy/django/
ExecStart=/usr/local/bin/gunicorn app.asgi:application --worker-class uvicorn.workers.UvicornWorker --pid /run/gunicorn/pid --bind unix:/run/gunicorn/socket --workers 4
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

Main problem with --preload is that reloading the service seems does not update the code, if we introduce changes to the code they are not visible, the log show the reload was successful but it does not take the new changes, maybe the workers don't get reloaded along with the main process.

Without the --preload it takes a long time to start for the first time, reloading works fine, but restarting the service doesn't work. Most of the log is this:

[INFO] Error while closing socket [Errno 9] Bad file descriptor
[INFO] Worker exiting (pid: 1211)

Please let me know what additional information do you need. Thank you.

@camilonova: I guess it needs some work on the gunicorn worker classes to rearrange when the event loop is setup.

First thing would just be to do a bit of investigative work along the lines of:

  • Does the gunicorn worker class provide an interface to allow code to run explicity before/after the prefork?
  • When is the event loop setup happening now, and when should it be happening?
  • Does aiohttp work with gunicorn preloading? If so, what are they doing in the worker class to allow it?
  • Does sanic work with gunicorn preloading? If so, what are they doing in the worker class to allow it?
  •