Closing python requests connection

后端 未结 1 1421
滥情空心
滥情空心 2020-12-19 06:20
import requests
requests.get(path_url, timeout=100)

In the above usage of python requests library, does the connection close automatically once req

相关标签:
1条回答
  • 2020-12-19 06:44

    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)
    
    0 讨论(0)
提交回复
热议问题