gevent

gevent/Event

  • Source Code

    import gevent
    from gevent.pywsgi import WSGIServer
    from gevent.queue import Queue, Empty
    
    data_source = Queue()
    
    def producer():
        while True:
            data_source.put_nowait('Hello World')
            gevent.sleep(1)
    
    def ajax_endpoint(environ, start_response):
        status = '200 OK'
        headers = [
            ('Content-Type', 'application/json')
        ]
    
        start_response(status, headers)
    
        while True:
            try:
                datum = data_source.get(timeout=5)
                yield datum + '\n'
            except Empty:
                pass
    
    gevent.spawn(producer)
    
    WSGIServer(('', 8000), ajax_endpoint).serve_forever()
    
  • Streaming tells clinet not close by adding Transfer Encoding: chunked in Response Headers

gevent + Django

  • Impossible if directly realize in Django
  • Django is request and resposne
  • Should start a extra service

References

[1] [email protected], Does Django have a way to open a HTTP long poll connection?

[2] [email protected], Reverse Ajax, Part 1: Introduction to Comet

results matching ""

    No results matching ""