Tornado AsyncHTTPClient 获取回调:额外参数?
我对整个异步游戏有点陌生(主要是 Django 人员),但我想知道:如何将额外的参数传递给 Tornado 的 AsyncHTTPClient.fetch 回调?例如,我正在跟踪回调被调用的次数(以便在处理数据之前等待一定数量的执行),并且我想做类似的事情:
def getPage(self, items,iteration):
http = AsyncHTTPClient()
http.fetch(feed, callback=self.resp(items,iteration))
def resp(self, response, items, iteration):
#do stuff
self.finish()
I'm sort of new to this whole async game (mostly been a Django guy), but I was wondering: how can I pass extra parameters to Tornado's AsyncHTTPClient.fetch callback? For example, I'm tracking the number of times a callback has been called (in order to wait until a certain number have executed before working on the data), and I'd like to do something like:
def getPage(self, items,iteration):
http = AsyncHTTPClient()
http.fetch(feed, callback=self.resp(items,iteration))
def resp(self, response, items, iteration):
#do stuff
self.finish()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要“绑定”您的附加参数。
使用 functools.partial,如下所示:
或者您可以使用 lambda,如下所示:(
您可能需要将签名添加到 def resp(self, items, iteration, response):)
You need to "bind" your additional arguments.
Use functools.partial, like this:
or you could use lambda, like this:
(you probably need to add the signature to def resp(self, items, iteration, response):)
如果您从内部调用 fetch,您还可以考虑 gen.coroutine 装饰器一个请求处理程序。在这种情况下,您无需向回调添加额外的参数,因为结果在与 fetch 调用相同的范围内可见。
you might also consider the gen.coroutine decorator if you're calling fetch from inside a RequestHandler. in that case, you have no need to add extra parameters to the callback because you have the result visible in the same scope as the call to fetch.