Created
April 18, 2018 05:50
-
-
Save marquisthunder/7dec44c193cbb8a553380aa995c43ae0 to your computer and use it in GitHub Desktop.
[featuresession]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| from concurrent.futures import ThreadPoolExecutor | |
| from threading import local | |
| class FuturesSession(requests.Session): | |
| def __init__(self, max_workers=2, session_factory=requests.Session, *args, **kwargs): | |
| super().__init__(*args, **kwargs) | |
| self.session_factory = session_factory | |
| self.session_args = args | |
| self.session_kwargs = kwargs | |
| self.sessions = [] | |
| self.executor = ThreadPoolExecutor(max_workers=max_workers) | |
| self.tls = local() | |
| def request(self, *args, **kwargs): | |
| def func(*args, **kwargs): | |
| session = getattr(self.tls, "session", None) | |
| if session is None: | |
| session = self.session_factory(*self.session_args, **self.session_kwargs) | |
| self.tls.session = session | |
| self.sessions.append(session) | |
| return session.request(*args, **kwargs) | |
| return self.executor.submit(func, *args, **kwargs) | |
| def close(self): | |
| self.executor.shutdown(wait=True) | |
| for session in self.sessions: | |
| session.close() | |
| super().close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment