Closing requests for python request

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

      

In the above using the python requests library, does the connection close automatically after request.get is started? If not how can I make sure the connection is closed

+3


source to share


1 answer


Yes, there is a call session.close

behind the code get

. If you're using a suitable IDE like PyCharm you can follow the code get

to see what's going on. Inside get

there is a request for a request:

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

      

The request

call is made in the definition of this method session.close

.



By following the link here for the repo request a call is made to control the session:

# 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)

      

+5


source







All Articles