当前位置: 动力学知识库 > 问答 > 编程问答 >

tornado (Python): persistent http connection

问题描述:

For http persistent connection I wrote the following code:

class LongPolling(tornado.web.RequestHandler):

waiters = set()

def get(self):

LongPolling.waiters.add(self)

for x in LongPolling.waiters:

x.write("Broadcast all")

x.flush()

return

def on_close(self):

logging.warning("Connection closed *********")

LongPolling.waiters.remove(self)

if __name__ == "__main__":

application = tornado.web.Application([

(r"/", LongPolling),

])

application.listen(8888)

tornado.ioloop.IOLoop.instance().start()

I am braodcasting every time a new connection comes.. But the problem with this is, immediately after get() the connection closes.

So how do I let the connection open after a get() call?

网友答案:

There is no such thing as a "persistent" http connection. the Connection: keep-alive header permits client and server to perform a new http request/response cycle without creating a new underlying tcp connection, to save a bit of network traffic, but that is not visible to the application; and usually implemented in the server side by a reverse proxy. clients will have to make new requests when they receive responses to their GET's.

If that's not what you had in mind, just that you want to respond to requests a bit at a time, then you might be looking for tornado.web.asynchronous. Note however, that most in-browser clients won't benefit from this very much; XHR's for instance won't be fired until the response completes, so browser applications will have to start a new request anyway

分享给朋友:
您可能感兴趣的文章:
随机阅读: