Close sessions created in the functional API
authorIan Cordasco <graffatcolmingov@gmail.com>
Fri, 7 Nov 2014 14:55:17 +0000 (08:55 -0600)
committerIan Cordasco <graffatcolmingov@gmail.com>
Fri, 7 Nov 2014 14:55:18 +0000 (08:55 -0600)
This is related to #1882 and #1685. By calling close on the session, we
clear the PoolManager operated by the Session and close all sockets.

Fixes #1882
Partially-fixes #1685

requests/api.py

index 4eaaf9e65144ad0fb9447463b6799ab2a064f25f..1469b05c4997c5347ee8b25d4598f0d4e06b58b6 100644 (file)
@@ -46,7 +46,12 @@ def request(method, url, **kwargs):
     """
 
     session = sessions.Session()
-    return session.request(method=method, url=url, **kwargs)
+    response = session.request(method=method, url=url, **kwargs)
+    # By explicitly closing the session, we avoid leaving sockets open which
+    # can trigger a ResourceWarning in some cases, and look like a memory leak
+    # in others.
+    session.close()
+    return response
 
 
 def get(url, **kwargs):