Tornado Web & Persistent Connections

二次信任 提交于 2020-01-13 11:35:35

问题


How can I write Http server in TornadoWeb that will support persistent Connections.

I mean will be able to receive many requests and answer to them without closing connection. How does it actually work in async?

I just want to know how to write handler to handle persistent connection. How actually would it work?

I have handler like that:

class MainHandler(RequestHandler):

count = 0
@asynchronous
def post(self):

    #get header content type 
    content_type = self.request.headers.get('Content-Type')
    if not content_type in ACCEPTED_CONTENT:
        raise HTTPError(403, 'Incorrect content type')
    text = self.request.body
    self.count += 1     

    command = CommandObject(text, self.count, callback = self.async_callback(self.on_response))
    command.execute()

def on_response(self, response):
    if response.error: raise HTTPError(500)
    body = response.body   
    self.write(body)
    self.flush()

execute calls callback when finishes.

is my asumption right that with things that way post will be called many times and for one connection count will increase with each httprequest from client? but for each connection I will have separate count value?


回答1:


I don't think that your assumption is correct. My understanding of the way the Tornado server works is that each request from the client will produce a new RequestHandler. The purpose of the @tornado.web.asynchronous decorator is to prevent the server from automatically closing the connection when your handler function (post, get, etc.) returns. But at the end of the day, I think there is just one response for each request.

I don't believe additional requests from the client will go to the same instance of the RequestHandler class. Instead, my understanding is that Tornado is set up to allow for the long-polling paradigm. Here is an example of the flow of communications:

  1. Client makes a POST request to the Tornado server
  2. Tornado server checks to see if a response is ready, if not you could add the RequestHandler to some kind of stack or queue (depending on your application architecture)
  3. Server comes up with a response (maybe another user added a message to the queue that needs to be distributed to open connections, etc.) and distributes the response back to the RequestHandler and then calls the finish() function to close the connection
  4. Client makes another POST request to repeat the process

I think if you want to implement true persistent connections you'll want to look into tornado.websocket (http://www.tornadoweb.org/documentation/websocket.html). I haven't experimented with that module yet so I'm afraid I can't give any input there.

Best of luck!




回答2:


The Tornado web framework actually does come with it's own server implementation which supports persistent connections, so there should be no need to write your own server. There is a section in the documentation on how to use it in production (behind nginx).




回答3:


From the source for tornado.web module, you can see that a new handler is always instantiated, I don't think there is anyway you can have handlers reused.



来源:https://stackoverflow.com/questions/1804013/tornado-web-persistent-connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!