mirror of
https://github.com/kennethreitz/requests3.git
synced 2026-06-05 23:10:16 +00:00
address connection leak issue from #520
* prefetch now defaults to True, ensuring that by default, sockets
are returned to the urllib3 connection pool on request end
* sessions now have a close() method, notifying urllib3 to close pooled
connections
* the module-level API, e.g., `requests.get('http://www.google.com')`,
explicitly closes its session when finished
When prefetch is False, the open socket becomes part of the state of the
Response object, and it's the client's responsibility to read the whole
body, at which point the socket will be returned to the pool.
This commit is contained in:
+12
-3
@@ -38,10 +38,19 @@ def request(method, url, **kwargs):
|
||||
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
|
||||
"""
|
||||
|
||||
s = kwargs.pop('session') if 'session' in kwargs else sessions.session()
|
||||
return s.request(method=method, url=url, **kwargs)
|
||||
|
||||
# if this session was passed in, leave it open (and retain pooled connections);
|
||||
# if we're making it just for this call, then close it when we're done.
|
||||
adhoc_session = False
|
||||
session = kwargs.pop('session', None)
|
||||
if session is None:
|
||||
session = sessions.session()
|
||||
adhoc_session = True
|
||||
|
||||
try:
|
||||
return session.request(method=method, url=url, **kwargs)
|
||||
finally:
|
||||
if adhoc_session:
|
||||
session.close()
|
||||
|
||||
def get(url, **kwargs):
|
||||
"""Sends a GET request. Returns :class:`Response` object.
|
||||
|
||||
Reference in New Issue
Block a user