I am using Gunicorn to deploy an Django app.
There is a global singleton variable defined in util.py:
global SNS_PRODUCERS
SNS_PRODUCERS = {}
def close_producers():
logger.info('Closing all producers...')
global SNS_PRODUCERS
for name, producer in SNS_PRODUCERS.items():
producer.close()
I would like to call close_producers
when my app shutdown. I added the following into gunicornconf.py:
def child_exit(server, worker):
# Join background threads in util.SNS_PRODUCERS
try:
from util import close_producers
close_producers()
except:
pass
def worker_exit(server, worker):
try:
from util import close_producers
close_producers()
except:
pass
My understanding is that, there is one master process, and multiple worker process forked from master process. In each process, util.py
is imported to this process. So, there are multiple SNS_PRODUCERS
in these process, one SNS_PRODUCERS
in one worker or master process.
Each worker process has ITS OWN worker_exit hook. The master process has ITS OWN child_exit hook.
When one worker is shutdown, its worker_exit
is called, and then ITS OWN SNS_PRODUCERS
is closed. Other SNS_PRODUCERS
are still open.
Am I right? Any corrections are welcomed.