ross / requests-futures

Asynchronous Python HTTP Requests for Humans using Futures
Other
2.11k stars 152 forks source link

Giving arguments to the callback #22

Closed JPFrancoia closed 9 years ago

JPFrancoia commented 9 years ago

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.
ross commented 9 years ago

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

JPFrancoia commented 9 years ago

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..