Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirantambe/ab106d42d5da438a0407abe41317de12 to your computer and use it in GitHub Desktop.
Save kirantambe/ab106d42d5da438a0407abe41317de12 to your computer and use it in GitHub Desktop.
Setting up gunicorn and celery with gevent, psycogreen for Django
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment