Skip to content Skip to sidebar Skip to footer

Tornado Asynchttpclient Fetch Callback: Extra Parameters?

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,

Solution 1:

You need to "bind" your additional arguments. Use functools.partial, like this:

items = ..
iteration = ..
cb = functools.partial(self.resp, items, iteration)

or you could use lambda, like this:

cb = lambda : self.resp(items, iteration)

(you probably need to add the signature to def resp(self, items, iteration, response):)

Solution 2:

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.

Post a Comment for "Tornado Asynchttpclient Fetch Callback: Extra Parameters?"