asyncron/asyncron/gunicorn.py

39 lines
1.3 KiB
Python

##
## - Gunicorn compatibility
## Add this to gunicorn.py conf file:
## from asyncron.gunicorn import post_fork
##
## adds an asyncron worker in each gunicorn worker process
## Hooks into 'dev reload' and 'exist signals' for graceful termination of tasks
##
def post_fork( server, worker ): #worker and AsyncronWorker, pay attention!
post_fork.server = server
post_fork.worker = worker
from .workers import AsyncronWorker
AsyncronWorker.log = worker.log
AsyncronWorker.log.info("Asyncron worker attached.")
init_to_override = AsyncronWorker.init
def init( *args, **kwargs ):
AsyncronWorker.MAX_COUNT = 1
AsyncronWorker.override_exit_signals()
to_override = worker.reloader._callback
def new_callback(*args, **kwargs):
AsyncronWorker.stop( reason = "Auto Reload" )
return to_override(*args, **kwargs)
worker.reloader._callback = new_callback
return init_to_override( *args, **kwargs )
AsyncronWorker.init = init
# Keeping the worker in post_fork.worker so we can add extra files it for it to track
# TODO: Currently unfinished, since i just realized using the "inotify" support of gunicorn
# makes this reduntant, but still here is the relevant code if I want to also support the simpler
# polling system
# Should be in asyncron.app.ready
# -> post_fork.worker.reloader.add_extra_file