Skip to content

Commit

Permalink
close session if it won't be reused (#138)
Browse files Browse the repository at this point in the history
* close session if it won't be reused
  • Loading branch information
spyoungtech authored Apr 5, 2020
1 parent dafe1a7 commit 7307426
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion grequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""
from functools import partial
import traceback

try:
import gevent
from gevent import monkey as curious_george
Expand All @@ -22,7 +23,6 @@

from requests import Session


__all__ = (
'map', 'imap',
'get', 'options', 'head', 'post', 'put', 'patch', 'delete', 'request'
Expand All @@ -47,6 +47,9 @@ def __init__(self, method, url, **kwargs):
self.session = kwargs.pop('session', None)
if self.session is None:
self.session = Session()
self._close = True
else:
self._close = False # don't close adapters after each request if the user provided the session

callback = kwargs.pop('callback', None)
if callback:
Expand All @@ -73,6 +76,11 @@ def send(self, **kwargs):
except Exception as e:
self.exception = e
self.traceback = traceback.format_exc()
finally:
if self._close:
# if we provided the session object, make sure we're cleaning up
# because there's no sense in keeping it open at this point if it wont be reused
self.session.close()
return self


Expand Down

0 comments on commit 7307426

Please sign in to comment.