19

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.

Community
  • 1
  • 1
Tereus Scott
  • 674
  • 1
  • 6
  • 11

1 Answers1

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
  • 148,955
  • 89
  • 346
  • 502
  • Thank you for the script, that is a big help. But, I have tried this and I think I am missing something. I am starting with the default django 1.4 project. I am starting the server with %python manage.py runserver 0.0.0.0:8000. How do I use the script you provided? – Tereus Scott Jun 13 '12 at 03:25
  • What part of its use are you unsure of? – David Wolever Jun 13 '12 at 03:33
  • Sorry to ask dumb questions.... how do I get this script to run when I start things up using manage.py? I am looking at the django wsgi docs right now and it mentions wsgi.py - is this where your script should go? – Tereus Scott Jun 13 '12 at 03:42
  • When I run the script above I get this raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE) So I added this: from django.conf import settings Now I get this: ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined. – Tereus Scott Jun 13 '12 at 03:50
  • I have no replaced wsgi.py with the contents of this script and I get this now: AttributeError: 'Settings' object has no attribute '__file__' – Tereus Scott Jun 13 '12 at 04:16
  • Oh, wait, I might have mixed up the order of the imports. I've edited my answer, give that a try. Put it in a file then run it. It will start an HTTP server listening on port 1234. – David Wolever Jun 13 '12 at 04:39
  • Make sure to check that your database connector supports running asynchronous inside gevent. For instance https://github.com/PyMySQL/mysqlclient-python does not. – Rune Kaagaard Mar 15 '20 at 10:47