New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add args to background callbacks #5
Comments
common python-ish solutions would to either use a closure or lambda i.e.
or
i haven't run across a situation with a pattern like background_callback_args in python, if you have/it's common i'd be open to the idea, just point me to some examples (unless lots of people are going to expect it i would prefer to avoid adding more ways to do things.) best, |
Lambda one works like a charm ;) Tnxs for the python-ish response. I think it is more adaptable and let a simpler code. |
cool. glad to help. |
Using the lambda doesn't quite work for me -- it's not really respecting the params I'm sending down (unless I'm having the wrong expectations): When running this test: def bg_call(sess, resp, hostname):
# host from response.
resp_host = urlparse.urlparse(resp.url).hostname.split('www.')[-1]
log.debug('orig hostname %s, from resp %s', hostname, resp_host)
reqs_num_workers = 10
session = FuturesSession(max_workers=reqs_num_workers)
targets = ['google.com', 'foo.com']
types = ['http://', 'https://']
for hostname in targets:
log.debug('doing %s', hostname)
for schema in types:
session.get('%s%s' % (schema, hostname),
background_callback=lambda sess, resp: bg_call(sess, resp, hostname),
timeout=2) I get this output (note how we end up with
|
i believe there's a common scope problem there. you're using in a variable hostname in the right side of the lambda that will have changed (many times) by the time the lambda is called. nothing specific to requests-futures, it's just a python scope thing. this stack overflow post seems to do a good job of describing the situation and solution: |
meet the same question, work out it followed as @ross
|
Background callback args are fixed for now, but I need pass some additional information to the callback in my code.
I suggest this request function in order to allow callbacks with arguments passed by the caller code.
Example usage:
The text was updated successfully, but these errors were encountered: