简体   繁体   English

关闭 requests.Session 重要吗?

[英]Is it important to close a requests.Session?

IN Python, when I create a requests.Session在 Python 中,当我创建requests.Session

eg例如

s = requests.session()
s.get('http://www.google.com')

Is it important to close the session afterwards?之后关闭会话重要吗?

eg例如

with requests.session() as s:
    s.get('http://www.google.com')

It all depends, as aways.这一切都取决于,因为远。

Definitely, closing resource that you allocate/open are a good practice, you have no reason to not do it.当然,关闭您分配/打开的资源是一种很好的做法,您没有理由不这样做。 Not closing connections may leave connections open and lead to too many open files on long term runs on linux environments, which is bad.不关闭连接可能会使连接保持打开状态,并导致在 linux 环境中长期运行时too many open files ,这很糟糕。 But if your script live for short term, then when the process terminates the OS reclaim the resources, so it would not be of great harm...但是如果你的脚本是短期存在的,那么当进程终止时操作系统会回收资源,所以它不会有太大的危害......

Take a look at Socket accept - "Too many open files" which is the error that you get on a connection leak看看Socket accept - “Too many open files”这是你在连接泄漏时遇到的错误

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

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