简体   繁体   中英

Is it important to close a requests.Session?

IN Python, when I create a 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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