From what I understand from the tornado.gen module, is that tornado.gen.Task consists of tornado.gen.Callback and tornado.gen.Wait with each callback / wait pair associated with unique keys ...
@tornado.web.asynchronous
@tornado.gen.engine
def get(self):
http_client = AsyncHTTPClient()
http_client.fetch("http://google.com",
callback=(yield tornado.gen.Callback("google")))
http_client.fetch("http://python.org",
callback=(yield tornado.gen.Callback("python")))
http_client.fetch("http://tornadoweb.org",
callback=(yield tornado.gen.Callback("tornado")))
response = yield [tornado.gen.Wait("google"), tornado.gen.Wait("tornado"), tornado.gen.Wait("python")]
do_something_with_response(response)
self.render("template.html")
Thus, the above code will receive all responses from different URLs. Now I really need to execute the response to return the response as soon as one http_client returns the data. Therefore, if "tornadoweb.org" first returns the data, it should do self.write (respose), and the loop in def get () should continue to wait for other http_clients to complete. Any ideas on how to write this using the tornado.gen interface.
A very vague implementation (and syntactically incorrect) of what I'm trying to do will be like this:
class GenAsyncHandler2(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self):
http_client = AsyncHTTPClient()
http_client.fetch("http://google.com",
callback=(yield tornado.gen.Callback("google")))
http_client.fetch("http://python.org",
callback=(yield tornado.gen.Callback("python")))
http_client.fetch("http://tornadoweb.org",
callback=(yield tornado.gen.Callback("tornado")))
while True:
response = self.get_response()
if response:
self.write(response)
self.flush()
else:
break
self.finish()
def get_response(self):
for key in tornado.gen.availableKeys():
if key.is_ready:
value = tornado.gen.pop(key)
return value
return None