require python-concurrent.futures only when needed
This commit is contained in:
parent
11ce95fd6a
commit
e3666afdfe
1 changed files with 10 additions and 3 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
#
|
#
|
||||||
# Copyright(C) 2014 smurail
|
# Copyright(C) 2014 Simon Murail
|
||||||
#
|
#
|
||||||
# This file is part of weboob.
|
# This file is part of weboob.
|
||||||
#
|
#
|
||||||
|
|
@ -20,7 +20,11 @@
|
||||||
# Inspired by: https://github.com/ross/requests-futures/blob/master/requests_futures/sessions.py
|
# Inspired by: https://github.com/ross/requests-futures/blob/master/requests_futures/sessions.py
|
||||||
# XXX Licence issues ?
|
# XXX Licence issues ?
|
||||||
|
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
try:
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
except ImportError:
|
||||||
|
ThreadPoolExecutor = None
|
||||||
|
|
||||||
from requests import Session
|
from requests import Session
|
||||||
from requests.adapters import DEFAULT_POOLSIZE, HTTPAdapter
|
from requests.adapters import DEFAULT_POOLSIZE, HTTPAdapter
|
||||||
|
|
||||||
|
|
@ -39,7 +43,7 @@ class FuturesSession(Session):
|
||||||
ignored and provided executor is used as is.
|
ignored and provided executor is used as is.
|
||||||
"""
|
"""
|
||||||
super(FuturesSession, self).__init__(*args, **kwargs)
|
super(FuturesSession, self).__init__(*args, **kwargs)
|
||||||
if executor is None:
|
if executor is None and ThreadPoolExecutor is not None:
|
||||||
executor = ThreadPoolExecutor(max_workers=max_workers)
|
executor = ThreadPoolExecutor(max_workers=max_workers)
|
||||||
# set connection pool size equal to max_workers if needed
|
# set connection pool size equal to max_workers if needed
|
||||||
if max_workers > DEFAULT_POOLSIZE:
|
if max_workers > DEFAULT_POOLSIZE:
|
||||||
|
|
@ -64,6 +68,9 @@ class FuturesSession(Session):
|
||||||
|
|
||||||
background_callback = kwargs.pop('background_callback', None)
|
background_callback = kwargs.pop('background_callback', None)
|
||||||
if background_callback:
|
if background_callback:
|
||||||
|
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 background_callback(self, resp)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue