Leave the connection open, until an event occurs.
相关问题
- Angular RxJS mergeMap types
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Django __str__ returned non-string (type NoneType)
Have a look at Django / Comet (Push): Least of all evils? or The latest recommendation for Comet in Python? - COMET is another name for "ajax long-polling".
Apparently the most common approach is not doing it directly in django but with the help of an additional daemon (probably because e.g. Apache doesn't do well with lots of long-living connections). Nowadays nodejs+socketio is pretty popular for this (and it can even use WebSockets) - you just need to find a nice way to pass data between the two things. If it's unidirectional (e.g. just broadcasts to all connected clients) a redis pubsub queue is not a bad option for this.
But http://code.google.com/p/django-orbited/ is probably the most djangoish solution.
I think the best way to asynchronous communication with Django is have a node server listening in another port and use the api client of Socket.io. In this way, you aren't dependent of the support of the modules for django and is very simple: Node listening the request from client, convert this request in a post request and send to Django for the port which listen Django. Is the best way i think.
server.js
views.py
index.html
For the future readers :)
I created a simple long polling django class views using Gevent, you can find it on github on https://github.com/tbarbugli/django_longpolling or get it from pypi (django_longpolling)
EDIT: I did some further experiment / deployment with django long polling / async workers and I can say that if possible opting to an external daemon is a very good choice, especially if you use the db (when using async worker you need a db connection pool or you are going to have the amount of worker connections bound to your db connection limits which is not desiderable).