Last active
January 30, 2024 09:26
-
-
Save jaddison/4506070 to your computer and use it in GitHub Desktop.
Setting up gunicorn and celery with gevent, psycogreen for Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# gunicorn.conf.py: getting gevent and psycogreen running | |
bind = '127.0.0.1:1437' | |
accesslog = "<some-path>/logs/gunicorn-access.log" | |
errorlog = "<some-path>/logs/gunicorn-error.log" | |
workers = 5 | |
try: | |
# fail 'successfully' if either of these modules aren't installed | |
from gevent import monkey | |
from psycogreen.gevent import patch_psycopg | |
# setting this inside the 'try' ensures that we only | |
# activate the gevent worker pool if we have gevent installed | |
worker_class = 'gevent' | |
# this ensures forked processes are patched with gevent/gevent-psycopg2 | |
def do_post_fork(server, worker): | |
monkey.patch_all() | |
patch_psycopg() | |
# you should see this text in your gunicorn logs if it was successful | |
worker.log.info("Made Psycopg2 Green") | |
post_fork = do_post_fork | |
except ImportError: | |
pass | |
# in your settings.py, along with the rest of your settings for celery: | |
CELERYD_POOL = 'gevent' | |
# if you want gevent and psycogreen to work for management commands, runserver | |
# and other things on the command line, add the following to the top of your manage.py | |
# like so (based on a 1.4.x Django installation): | |
try: | |
from gevent import monkey | |
monkey.patch_all() | |
except ImportError: | |
pass | |
try: | |
from psycogreen.gevent import patch_psycopg | |
patch_psycopg() | |
except ImportError: | |
pass | |
# ... the rest of manage.py is here |
Gunicorn already calls monkey.patch_all() after do_post_fork(). You can instead override post_worker_init(). At that point, the patching would be done, so you could call only patch_psycopg(), and remove the call to monkey.patch_all().
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i want to only run it for celery so any example where to set post_fork hook