requests-futures只是concurrent.futures顶部的一个非常小的包装。您可以通过查看source code(为简洁起见,删除了docstrings)来看到这一点:from concurrent.futures import ThreadPoolExecutor
from requests import Session
from requests.adapters import DEFAULT_POOLSIZE, HTTPAdapter
class FuturesSession(Session):
def __init__(self, executor=None, max_workers=2, *args, **kwargs):
super(FuturesSession, self).__init__(*args, **kwargs)
if executor is None:
executor = ThreadPoolExecutor(max_workers=max_workers)
# set connection pool size equal to max_workers if needed
if max_workers > DEFAULT_POOLSIZE:
adapter_kwargs = dict(pool_connections=max_workers,
pool_maxsize=max_workers)
self.mount('https://', HTTPAdapter(**adapter_kwargs))
self.mount('http://', HTTPAdapter(**adapter_kwargs))
self.executor = executor
def request(self, *args, **kwargs):
func = sup = super(FuturesSession, self).request
background_callback = kwargs.pop('background_callback', None)
if background_callback:
def wrap(*args_, **kwargs_):
resp = sup(*args_, **kwargs_)
background_callback(self, resp)
return resp
func = wrap
return self.executor.submit(func, *args, **kwargs) # This returns a concurrent.futures.Future
当您使用requests-futures时,实际上是在使用concurrent.futures.ThreadPoolExecutor,当您submit一个任务时,它返回一个concurrent.futures.Future。如果使用requests-futures提供的API来处理HTTP请求更方便,那么可以坚持使用它,甚至可以将它返回的对象与concurrent.futures模块提供的其他方法一起使用。在