Tornado streaming HTTP response as AsyncHTTPClient receives chunks

后端 未结 1 512
醉酒成梦
醉酒成梦 2020-12-30 05:51

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

1条回答
  •  时光说笑
    2020-12-30 06:16

    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.

    0 讨论(0)
提交回复
热议问题