Here's how I run Django with gevent + monkey patching:
I've modified manage.py
so the first line (after the shebang) is from gevent import monkey; monkey.patch_all()
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…