Browser: callback in the same place, async or not
This commit is contained in:
parent
25da89642a
commit
f591dba678
2 changed files with 15 additions and 16 deletions
|
|
@ -346,10 +346,8 @@ class Browser(object):
|
||||||
verify=verify,
|
verify=verify,
|
||||||
cert=cert,
|
cert=cert,
|
||||||
proxies=proxies,
|
proxies=proxies,
|
||||||
background_callback=async and inner_callback)
|
callback=inner_callback,
|
||||||
if not async:
|
async=async)
|
||||||
inner_callback(self, response)
|
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def async_open(self, url, **kwargs):
|
def async_open(self, url, **kwargs):
|
||||||
|
|
|
||||||
|
|
@ -131,25 +131,26 @@ class FuturesSession(WeboobSession):
|
||||||
|
|
||||||
Used by :meth:`request` and thus all of the higher level methods
|
Used by :meth:`request` and thus all of the higher level methods
|
||||||
|
|
||||||
If background_callback param is defined, request is processed in a
|
If the `async` param is True, the request is processed in a
|
||||||
thread, calling background_callback and returning it's result when
|
thread. Otherwise, the request is processed as usual, in a blocking way.
|
||||||
request has been processed. If background_callback is not defined,
|
|
||||||
request is processed as usual, in a blocking way.
|
In all cases, it will call the `callback` parameter and return its
|
||||||
|
result when the request has been processed.
|
||||||
"""
|
"""
|
||||||
sup = super(FuturesSession, self).send
|
sup = super(FuturesSession, self).send
|
||||||
|
|
||||||
background_callback = kwargs.pop('background_callback', None)
|
callback = kwargs.pop('callback', lambda future, response: response)
|
||||||
if background_callback:
|
async = kwargs.pop('async', False)
|
||||||
if not self.executor:
|
|
||||||
raise ImportError('Please install python-concurrent.futures')
|
|
||||||
|
|
||||||
def func(*args, **kwargs):
|
def func(*args, **kwargs):
|
||||||
resp = sup(*args, **kwargs)
|
resp = sup(*args, **kwargs)
|
||||||
return background_callback(self, resp)
|
return callback(self, resp)
|
||||||
|
|
||||||
|
if async:
|
||||||
|
if not self.executor:
|
||||||
|
raise ImportError('Please install python-concurrent.futures')
|
||||||
return self.executor.submit(func, *args, **kwargs)
|
return self.executor.submit(func, *args, **kwargs)
|
||||||
|
|
||||||
return sup(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
super(FuturesSession, self).close()
|
super(FuturesSession, self).close()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue