Browser: callback in the same place, async or not

This commit is contained in:
Laurent Bachelier 2015-04-09 10:15:57 +02:00 committed by Romain Bignon
commit f591dba678
2 changed files with 15 additions and 16 deletions

View file

@ -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):

View file

@ -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)
def func(*args, **kwargs):
resp = sup(*args, **kwargs)
return callback(self, resp)
if async:
if not self.executor: if not self.executor:
raise ImportError('Please install python-concurrent.futures') raise ImportError('Please install python-concurrent.futures')
def func(*args, **kwargs):
resp = sup(*args, **kwargs)
return background_callback(self, resp)
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()