龙卷风流HTTP响应为AsyncHTTPClient接收块

龙卷风流HTTP响应为AsyncHTTPClient接收块

本文介绍了龙卷风流HTTP响应为AsyncHTTPClient接收块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个龙卷风请求处理程序,因为它接收从它的异步请求,这使得异步HTTP请求,并返回数据给客户端。不幸的是,我无法获得龙卷风任何数据返回给客户端,直到它的所有异步HTT prequests已经完成。

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. Unfortunately, I'm unable to get Tornado to return any data to the client until all of it's Async HTTPRequests have completed.

我的请求处理程序的演示如下。

A demo of my request handler is below.


class StreamingHandler(web.RequestHandler):

    all_requested = False
    requests = []

    @web.asynchronous
    def get(self):

        http_client = httpclient.AsyncHTTPClient()
        self.write('some opening')

        big_request = httpclient.HTTPRequest(url='[some_big_request]', streaming_callback=self.on_chunk)
        small_request = httpclient.HTTPRequest(url='[some_small_request]', streaming_callback=self.on_chunk)

        self.requests.append(http_client.fetch(big_request, callback=self.on_response_complete))
        self.requests.append(http_client.fetch(small_request, callback=self.on_response_complete))

        self.all_requested = True

    def on_chunk(self, chunk):
        self.write('some chunk')
        self.flush()

    def on_response_complete(self, response):
        if self.all_requested and all(request.done() for request in self.requests):
            self.write('some closing')
            self.finish()

我所期望的GET请求,这个处理程序最初是为更大的返回文本'开场白',然后很快返回一些大块'为小的要求,后来又回某些块'(可能多次)要求,最后才返回一些收,并关闭连接。相反,建立连接后,客户端将等待所有请求完成了几秒钟,然后接收所有HTT presponse的同时,关闭之前。

I would expect a GET request to this handler to initially return the text 'some opening', then quite quickly return 'some chunk' for the small request, and later return 'some chunk' (potentially multiple times) for the larger request, before finally returning 'some closing', and closing the connection. Instead, after making the connection, the client waits a few seconds for all requests to complete, and then receives all of the HTTPResponse at once, before closing.

我怎么会去从龙卷风让我的期望行为?

How would I go about getting my desired behaviour from Tornado?

在此先感谢!

推荐答案

装饰用 gen.coroutine 你的方法和产量期货的列表。这里有一个简单的例子:

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()

注意,即使请求产生向后,第一个块仍然会后约一第二接收。如果你送他们出去同步,它会带你15秒。当你异步请求他们,这需要你只需5。

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.

这篇关于龙卷风流HTTP响应为AsyncHTTPClient接收块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 22:40