G
G
GilbertAmethyst2018-07-14 14:15:54
Python
GilbertAmethyst, 2018-07-14 14:15:54

How to pass an additional argument to a callback function?

Hello.
There is something like this python code:

def response_handler(response, **kwargs):
    print(response.url)

urls = [
{"id":1, "url":"http://httpbin.org/delay/1"},
{"id":2, "url":"http://httpbin.org/delay/1"}
]

results = grequests.map((grequests.get(u["url"], callback=response_handler) for u in urls))

Task:
It is necessary to additionally pass the u["id"] parameter to the response_handler callback function in order to finally know which array object was received. Tried lambda function but error appears:
<lambda>() got an unexpected keyword argument 'timeout'

Tell me how to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-07-14
@GilbertAmethyst

def response_handler(response, url_id):
    print(response.url)

results = grequests.map(
  (grequests.get(u["url"], callback=lambda r, uid=u['id'], **kw: response_handler(r, uid)) for u in urls))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question