Cara Esten Hurtle
Cara Esten Hurtle

Reputation: 257

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

Upvotes: 17

Views: 4375

Answers (2)

user2571090
user2571090

Reputation: 31

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.

Upvotes: 0

Schildmeijer
Schildmeijer

Reputation: 20946

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

Upvotes: 22

Related Questions