Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing python requests connection

import requests
requests.get(path_url, timeout=100)

In the above usage of python requests library, does the connection close automatically once requests.get is done running? If not, how can I make for certain that connection is closed

like image 465
user308827 Avatar asked Jun 21 '17 02:06

user308827


1 Answers

Yes, there is a call to a session.close behind the get code. If using a proper IDE like PyCharm for example, you can follow the get code to see what is happening. Inside get there is a call to request:

return request('get', url, params=params, **kwargs)

Within the definition of that request method, the call to session.close is made.

By following the link here to the requests repo, there is a call being made for the session control:

# By using the 'with' statement we are sure the session is closed, thus we
# avoid leaving sockets open which can trigger a ResourceWarning in some
# cases, and look like a memory leak in others.
with sessions.Session() as session:
    return session.request(method=method, url=url, **kwargs)
like image 76
idjaw Avatar answered Nov 21 '22 01:11

idjaw