I\'m trying to write a Tornado request handler which makes asynchronous HTTP requests, and returns data to the client as it receives it from it\'s async requests. Unfortunat
Decorate your method with gen.coroutine
and yield a list of futures. Here's a simple example:
from tornado import gen, web, httpclient
class StreamingHandler(web.RequestHandler):
@web.asynchronous
@gen.coroutine
def get(self):
client = httpclient.AsyncHTTPClient()
self.write('some opening')
self.flush()
requests = [
httpclient.HTTPRequest(
url='http://httpbin.org/delay/' + str(delay),
streaming_callback=self.on_chunk
) for delay in [5, 4, 3, 2, 1]
]
# `map()` doesn't return a list in Python 3
yield list(map(client.fetch, requests))
self.write('some closing')
self.finish()
def on_chunk(self, chunk):
self.write('some chunk')
self.flush()
Notice that even though the requests are yielded "backwards", the first chunk will still be received after about a second. If you sent them out synchronously, it'd take you 15 seconds. When you request them asynchronously, it takes you just 5.