Using Tornado, I have a Get request that takes a long time as it makes many requests to another web service and processes the data, could take minutes to fully complete. I don't want this to block the entire web server from responding to other requests, which it currently does.
As I understand it, Tornado is single threaded and executes each request synchronously, even though it handles them asynchronously (still confused on that bit). There are parts of the long process that could be pause points to allow the server to handle other requests (possible solution?). I'm running it on Heroku with a single worker, so not sure how that translates into spawning a new thread or multiprocessing, which I have no experience in with python.
Here is what I'm trying to do: the client makes the get call to start the process, then I loop through another get call every 5 seconds to check the status and update the page with new information (long polling would also work but running into the same issue). Problem is that starting the long process blocks all new get requests (or new long polling sessions) until it completes.
Is there an easy way to kick off this long get call and not have it block the entire web server in the process? Is there anything I can put in the code to say.. "pause, go handle pending requests then continue on"?
I need to initiate a get request on ProcessHandler. I then need to continue to be able to query StatusHandler while ProcessHandler is running.
Example:
class StatusHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
self.render("status.html")
class ProcessHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
self.updateStatus("0")
result1 = self.function1()
self.updateStatus("1")
result2 = self.function2(result1)
self.updateStatus("2")
result3 = self.function3(result2)
self.updateStatus("3")
self.finish()