3

IN Python, when I create a requests.Session

e.g.

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

Is it important to close the session afterwards?

e.g.

with requests.session() as s:
    s.get('http://www.google.com')
SuperShoot
  • 9,880
  • 2
  • 38
  • 55
Ginger
  • 8,320
  • 12
  • 56
  • 99
  • None of the examples in [the docs](http://docs.python-requests.org/en/master/user/advanced/#session-objects) explicitly call close, but there is a context manager documented that implicitly does so. If it is important, perhaps those examples should demonstrate it too. – SuperShoot Mar 26 '19 at 10:19

1 Answers1

2

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

geckos
  • 5,687
  • 1
  • 41
  • 53