问题描述
这是我从tornado.gen模块文档明白的是,tornado.gen.Task包括与每次回调tornado.gen.Callback和tornado.gen.Wait的/等待具有独特的键相关的一...
@ tornado.web.asynchronous
@ tornado.gen.engine
DEF得到(个体经营):
http_client = AsyncHTTPClient()
http_client.fetch(http://google.com
回调=(收益率tornado.gen.Callback(谷歌))) http_client.fetch(http://python.org
回调=(收益率tornado.gen.Callback(蟒蛇))) http_client.fetch(http://tornadoweb.org
回调=(收益率tornado.gen.Callback(龙卷风)))
响应=产量[tornado.gen.Wait(谷歌),tornado.gen.Wait(龙卷风),tornado.gen.Wait(蟒蛇)] do_something_with_response(响应)
self.render(template.html)
所以上面的code会从不同的URL的所有响应。
现在我真的需要做到的是尽快为一体http_client返回数据返回的响应。所以,如果tornadoweb.org首先返回的数据,应该做一个self.write(respose)和DEF得到一个循环()应该保持等待其他http_clients来完成。
如何写这个使用tornado.gen接口的任何想法。
什么我试图做的非常模糊的实现(语法不正确的)会是这样
类GenAsyncHandler2(tornado.web.RequestHandler):
@ tornado.web.asynchronous
@ tornado.gen.engine
DEF得到(个体经营):
http_client = AsyncHTTPClient()
http_client.fetch(http://google.com
回调=(收益率tornado.gen.Callback(谷歌))) http_client.fetch(http://python.org
回调=(收益率tornado.gen.Callback(蟒蛇))) http_client.fetch(http://tornadoweb.org
回调=(收益率tornado.gen.Callback(龙卷风))) 而真正的:
响应= self.get_response()
如果响应:
self.write(响应)
self.flush()
其他:
打破
self.finish()
高清get_response(个体经营):
在tornado.gen.availableKeys()键:
如果key.is_ready:
值= tornado.gen.pop(键)
返回值
返回None
除此之外,其实还有一种方法为WaitAll当所有HTTPCliens完成赋予它的响应等待所有的结果和回报。
我已经提交的差异在我的龙卷风分支()。我添加了一个类了WaitAny是异步为WaitAll并返回尽快导致为一体了HTTPClient返回结果。
DIFF是(的), (https://github.com/pranjal5215/tornado/wiki/Add-WaitAny-to-make-WaitAll-return-results-incrementally)
使用范例:
类GenAsyncHandler2(tornado.web.RequestHandler):
@ tornado.web.asynchronous
@ tornado.gen.engine
DEF得到(个体经营):
http_client = AsyncHTTPClient()
http_client.fetch(http://google.com
回调=(收益率tornado.gen.Callback(谷歌))) http_client.fetch(http://python.org
回调=(收益率tornado.gen.Callback(蟒蛇))) http_client.fetch(http://tornadoweb.org
回调=(收益率tornado.gen.Callback(龙卷风)))
键=集([谷歌,龙卷风,巨蟒])
而键:
键,响应=产量tornado.gen.WaitAny(键)
keys.remove(键)
#做一些回应
self.write(STR(键)+)
self.flush()
self.finish()
From what I understand from tornado.gen module docs is that tornado.gen.Task comprises 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")
So the above code will get all responses from the different URLs.Now what I actually need to accomplish is to return the response as soon as one http_client returns the data. So if 'tornadoweb.org' returns the data first, it should do a self.write(respose) and a loop in def get() should keep waiting for other http_clients to complete.Any ideas on how to write this using tornado.gen interface.
Very vague implementation(and syntactically incorrect) of what I am trying to do would 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
In addition to this, actually there is a method WaitAll which waits for all results and returns when all HTTPCliens have completed giving responses.I have submitted the diff in my tornado branch (https://github.com/pranjal5215/tornado). I have added a class WaitAny which is async WaitAll and returns result as soon as one HTTPClient has returned result.
Diff is at (https://github.com/pranjal5215/tornado/commit/dd6902147ab2c5cbf2b9c7ee9a35b4f89b40790e), (https://github.com/pranjal5215/tornado/wiki/Add-WaitAny-to-make-WaitAll-return-results-incrementally)
Sample usage:
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")))
keys = set(["google", "tornado", "python"])
while keys:
key, response = yield tornado.gen.WaitAny(keys)
keys.remove(key)
# do something with response
self.write(str(key)+" ")
self.flush()
self.finish()
这篇关于龙卷风异步HTTP返回结果增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!