[Fixed]-How to combine django plus gevent the basics?

24đź‘Ť

âś…

Here’s how I run Django with gevent + monkey patching:

  1. I’ve modified manage.py so the first line (after the shebang) is from gevent import monkey; monkey.patch_all()

  2. I’ve added a new run_production_server script (see below).

Finally, I’ve configured my front-end webserver to proxy requests to port 1234 (the port which run_production_server is listening on).

from gevent import monkey; monkey.patch_all()
from gevent.wsgi import WSGIServer

from django.core.management import setup_environ    
import settings
setup_environ(settings)

from django.core.handlers.wsgi import WSGIHandler as DjangoWSGIApp
application = DjangoWSGIApp()
server = WSGIServer(("127.0.0.1", 1234), application)
print "Starting server on http://127.0.0.1:1234"
server.serve_forever()

Some might complain that this server isn’t “web scale” enough. I doubt they would be able to provide benchmarks to prove that, but if you’re worried you could also use gunicorn or uwsgi for your server. But this works just fine for me.

👤David Wolever

Leave a comment