[Django]-Gunicorn Django Threading

5đź‘Ť

âś…

No. There is nothing special about threads spawned from Gunicorn worker main threads.

Once you spawn a thread, it will execute in parallel until completion or death. Gunicorn doesn’t know about these threads spawned from worker main threads, so it doesn’t try to join on them, therefore the worker main threads will not wait for child thread completion. Also, the daemon-ness of the thread has no effect; daemon simply means that the thread won’t contribute to the “alive-ness” of the process, and will run until process exit, when it will be automatically killed.

If you want to wait for these threads to complete before re-using the same worker thread, you have to do that before the WSGI application (e.g. django.core.handlers.wsgi.WSGIHandler.__call__()) returns. Or write some crazy monkey-patch for Gunicorn to keep track of child threads.

TL;DR you can definitely grow threads without bound by spawning long-running child threads from worker main threads. Best to guarantee that they will finish within some time bound with timeouts.

👤Ben Simmons

Leave a comment