Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to combine django plus gevent the basics?

After much searching and googling I am coming back to the well. I have Django 1.4 and am looking for a decent working example to figure out getting Django to work with gevent. I like the Django framwork but I need it to handle long polling. I already have a working server using gevent on it's own that handles long polling requests as well as does image streaming via http at about 10 frames/second. I would like to use all the goodies in Django to provide a framework for this part.

There are many examples out there, but unfortunately none of these seem to work out of the box! It would really help to have a working example to understand how these two things are working together.

Here is what I have found so far and the problems:

http://codysoyland.com/2011/feb/6/evented-django-part-one-socketio-and-gevent/ problem: ImportError: Could not import settings 'webchat.settings' (Is it on sys.path?): No module named webchat.settings

https://github.com/codysoyland/django-socketio-example/blob/master/README.rst Problem: installation fails with permission problem getting gevent Tried manually getting it from git hub. The example runs, but generates these errors when the browsers connect.

These are informative but do not provide the basic answer. Need help understanding Comet in Python (with Django) https://bitbucket.org/denis/gevent/src/tip/examples/webchat/chat/views.py http://blog.gevent.org/2009/10/10/simpler-long-polling-with-django-and-gevent/

What I hope someone can explain (please, pretty please....) is this: I have a basic site created using Django 1.4 - the tutorial here https://docs.djangoproject.com/en/1.4/intro/tutorial01/ is excellent. So now I need to understand what changes to make in order to use gevent and be able to handle asynchronous events. I am sure it is not difficult - I just need someone who understands it to explain what to do and also what is happening (with things like monkey_patch).

Thanks.

like image 450
Tereus Scott Avatar asked Jun 09 '12 21:06

Tereus Scott


1 Answers

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.

like image 71
David Wolever Avatar answered Oct 06 '22 04:10

David Wolever