Search code examples
pythoncomettornadolong-polling

Tornado - How to implement long polling client


I'm trying to implement long pulling client in Tornado, that interacts with an asynchronous Tornado server.

What happens is one of 2 things:

  1. Either the client timesout, or
  2. The client receives all the messages at once after finishing the whole background process, similar to blocking ones

This is the client I use:

from tornado import ioloop
from tornado import httpclient

print "\nNon-Blocking AsyncHTTPClient"
import tornado.ioloop

def async_call(response):
        if response.error:
                response.rethrow()
        print "AsyncHTTPClient Response"
        ioloop.IOLoop.instance().stop()

http_client = httpclient.AsyncHTTPClient()
http_client.fetch("http://localhost:9999/text/", async_call)
ioloop.IOLoop.instance().start()

Is this the right way to write a long-polling/comet client?

I would also appreciate for those who will answer to provide a sample async-server in Tornado, because may be I'm writing the cometed Tornado server wrongly... I'm a bit new to the whole long-polling process in general.


Solution

  • Tornado itself has an excellent example of chat, built on top of long-polling mechanism

    https://github.com/facebook/tornado/tree/master/demos/chat

    It helped me a lot to understand everything, and it have both server and client.