<p>您看到的是在这种情况下使用协同程序的一个缺点。因为即使请求是非阻塞的,它们也不是真正的异步的。你知道吗</p>
<p>您可以使用<a href="http://www.tornadoweb.org/en/stable/gen.html#tornado.gen.WaitIterator" rel="nofollow noreferrer">^{<cd1>}</a>一次发送所有请求,真正地异步发送。你知道吗</p>
<p>正如文件所说:</p>
<blockquote>
<p>If you need to get the result of each future as soon as possible, or if you need the result of some futures even if others produce errors, you can use <code>WaitIterator</code>.</p>
</blockquote>
<p>它完全符合您的用例。你知道吗</p>
<p><code>gen.WaitIterator</code>将任意数量的期货作为参数,并在期货结束时生成期货。你知道吗</p>
<p>但是,要使其工作,您需要将<code>for</code>循环移动到<code>go</code>协同程序中。示例:</p>
<pre><code>from tornado import gen
async def go(r):
client = AsyncHTTPClient()
futures = []
for _ in range(r):
print('go ')
request = HTTPRequest(url='http://127.0.0.1:8001/api', method='GET',)
futures.append(client.fetch(request))
async for result in gen.WaitIterator(*futures):
print(result.code, result.body)
</code></pre>
<hr/>
<p>顺便说一句,还有另一种方法可以通过回调来实现这一点。然而,<code>WaitIterator</code>应该优先于回调。如果你感兴趣的话,我前一段时间写了一篇<a href="https://bhch.github.io/posts/2017/10/making-multiple-async-http-requests-using-tornado/" rel="nofollow noreferrer">blog post</a>关于这个的文章。你知道吗</p>