How to integrate Gevent with Django framework, What settings has to be modified in the Settings.py and wsgi.py files for the integration.
I want to start gevent http server(port 8000) and gevent web socket server(port 9000) from the Django.
How to integrate Gevent with Django framework, What settings has to be modified in the Settings.py and wsgi.py files for the integration.
I want to start gevent http server(port 8000) and gevent web socket server(port 9000) from the Django.
The easiest way is to use Gunicorn and tell it to use the gevent worker class. The Gunicorn docs are pretty good. For Django 1.4 or later the recommended way to start Gunicorn is simply to call the WSGI interface like so:
gunicorn --worker-class gevent wsgi:application
If you don't care about all the nice features of Gunicorn (like graceful restarts for no-downtime upgrades for example) you can use the gevent wsgi server directly. I do this myself to save some memory for non-critical websites that can be down for a little while during upgrades. This is my "run_gevent.py" file, it should be fairly easy to grok:
import gevent.monkey; gevent.monkey.patch_all()
import os, socket
from gevent.socket import socket as gsocket
from gevent.pywsgi import WSGIServer
from django.core.handlers.wsgi import WSGIHandler
script_dir = os.path.dirname(os.path.abspath(__file__))
pid_filename = os.path.join(script_dir, 'server.pid')
socket_filename = os.path.join(script_dir, 'server.sock')
pidfile = open(pid_filename, 'w')
pidfile.write(str(os.getpid()) + str('\n'))
pidfile.close()
server_socket = gsocket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
os.remove(socket_filename)
except OSError:
pass
server_socket.bind(socket_filename)
server_socket.listen(256)
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
server = WSGIServer(listener = server_socket, application = WSGIHandler(), log = None)
server.serve_forever(stop_timeout = 3)