Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

python - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...