Skip to content

Instantly share code, notes, and snippets.

@ichux
Forked from tmc/gunicorn.conf.py
Created December 6, 2020 16:30
Show Gist options
  • Save ichux/743f9545d051cb92838f978a98c42844 to your computer and use it in GitHub Desktop.
Save ichux/743f9545d051cb92838f978a98c42844 to your computer and use it in GitHub Desktop.
import logging
import os
import signal
import sys
def when_ready(server):
if server.worker_class.__name__ != 'GeventWorker':
server.log.info('Skipping code reloading since worker is not gevent.')
return
def monitor():
modify_times = {}
while True:
for module in sys.modules.values():
path = getattr(module, "__file__", None)
if not path: continue
if path.endswith(".pyc") or path.endswith(".pyo"):
path = path[:-1]
try:
modified = os.stat(path).st_mtime
except:
continue
if path not in modify_times:
modify_times[path] = modified
continue
if modify_times[path] != modified:
logging.info("%s modified; restarting server", path)
os.kill(os.getpid(), signal.SIGHUP)
modify_times = {}
break
gevent.sleep(1)
import gevent
gevent.spawn(monitor)
profiling_prefix = '.profile.'
enable_profiling = False
if enable_profiling:
import cProfile
def post_fork(server, worker):
orig_init_process_ = worker.init_process
def profiling_init_process(self):
orig_init_process = orig_init_process_
ofile = '%s%s' % (profiling_prefix, os.getpid())
server.log.info('Profiling worker %s, output file: %s' % (worker, ofile))
cProfile.runctx('orig_init_process()', globals(), locals(), ofile)
worker.init_process = profiling_init_process.__get__(worker)
worker_class = 'gevent'
log_file = '-'
loglevel = 'debug'
timeout = 60
bind = '0.0.0.0:8000'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment