简体   繁体   English

关闭python请求连接

[英]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?上面python请求库的使用中,requests.get运行完成后连接会自动关闭吗? If not, how can I make for certain that connection is closed如果没有,我如何确定连接已关闭

Yes, there is a call to a session.close behind the get code.是的,在get代码后面有一个对session.close的调用。 If using a proper IDE like PyCharm for example, you can follow the get code to see what is happening.例如,如果使用像 PyCharm 这样合适的 IDE,您可以按照get代码查看发生了什么。 Inside get there is a call to request:get里面有一个请求:

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

Within the definition of that request method, the call to session.close is made.在该request方法的定义中,调用session.close

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM