Requests-futures: Giving arguments to the callback

Created on 20 Feb 2015  路  2Comments  路  Source: ross/requests-futures

Hi, I wonder if there is a way to give arguments to the callback through add_done_callback.

def print_result(future):
    response = future.result()
    print(response.url, response.status_code)


for url in list_urls:
    future = session.get(url)
    future.add_done_callback(print_result)

I would like to pass arguments, something like:

    future.add_done_callback(lambda: print_result(str1, str2))

Is it possible ?

From what I read from the doc, it is not, but it would be very convenient:

add_done_callback(self, fn)
 |      Attaches a callable that will be called when the future finishes.
 |      
 |      Args:
 |          fn: A callable that will be called with this future as its only
 |              argument when the future completes or is cancelled. The callable
 |              will always be called by a thread in the same process in which
 |              it was added. If the future has already completed or been
 |              cancelled then the callable will be called immediately. These
 |              callables are called in the order that they were added.

Most helpful comment

Ok thanks. I alos found a solution here actually:
https://docs.python.org/3/library/asyncio-task.html

        future.add_done_callback(functools.partial(print_result, "papa", "maman"))

Just a few minutes after I opened the issue..

All 2 comments

duplicate of https://github.com/ross/requests-futures/issues/5, please take a look there

Ok thanks. I alos found a solution here actually:
https://docs.python.org/3/library/asyncio-task.html

        future.add_done_callback(functools.partial(print_result, "papa", "maman"))

Just a few minutes after I opened the issue..

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thelink2012 picture thelink2012  路  5Comments

spex66 picture spex66  路  11Comments

earada picture earada  路  7Comments

gpetukhov picture gpetukhov  路  3Comments

AndreaCrotti picture AndreaCrotti  路  3Comments