简体   繁体   中英

Tornado request timeouts

I have a Tornado Web application which connects to an external app server on a TCP port on localhost. Sometimes, if the app server responds slowly, then even requests to the Tornado server which do not talk to the app server become slow as they are held up for the other requests to finish. What's the right way to handle this?

Thanks, Ashish

What I usually do in such scenarios is start a new Thread on each request(in handlers that may take longer than 500 ms). For this, you need to declare your method (eg get) as asynchronous using @asynchronous decorator. See here for more details.

Once you declare a request handler method as asynchronous, you have to call self.finish() yourself. Otherwise tornado would call it itself after the method is executed. See async networking

The following is a code snippet that starts a Thread and let it handle the request...

class MainRequestHandler(tornado.web.RequestHandler):
    @asynchronous
        def post(self):
        MainRequestThread(self).start()

class MainRequestThread(threading.Thread):
    def __init__(self, request = None, *args, **kwargs):
        super(MainRequestThread, self).__init__(*args, **kwargs)
        self.request = request

    def run(self):
        #Do some long processing here...
        self.request.finish("Some cool message") # self.request has the self of RequestHandler here

Note: Avoid using the same structure on every request handler. Creating Threads has overhead too.

As user2877889 mentioned, you want to make your RequestHandlers asynchronous to make sure that other requests are not queued up because of a long running request. But at the same time, it is also important to note that if you are getting a timeout right now, you may get it after making it non-blocking or asynchronous as well.

What you want to do is increase your timeout while you are sending request as well. Whatever way you are using to send request there should be a way to set the timeout. In jquery you can do it like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM