Codebase list python-grequests / 7307426
close session if it won't be reused (#138) * close session if it won't be reused Spencer Phillip Young authored 4 years ago GitHub committed 4 years ago
1 changed file(s) with 9 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
99 """
1010 from functools import partial
1111 import traceback
12
1213 try:
1314 import gevent
1415 from gevent import monkey as curious_george
2021 curious_george.patch_all(thread=False, select=False)
2122
2223 from requests import Session
23
2424
2525 __all__ = (
2626 'map', 'imap',
4646 self.session = kwargs.pop('session', None)
4747 if self.session is None:
4848 self.session = Session()
49 self._close = True
50 else:
51 self._close = False # don't close adapters after each request if the user provided the session
4952
5053 callback = kwargs.pop('callback', None)
5154 if callback:
7275 except Exception as e:
7376 self.exception = e
7477 self.traceback = traceback.format_exc()
78 finally:
79 if self._close:
80 # if we provided the session object, make sure we're cleaning up
81 # because there's no sense in keeping it open at this point if it wont be reused
82 self.session.close()
7583 return self
7684
7785