Will a request.Session close automatically when my script exits

Viewed 903

I have this code in my script

sess = requests.Session()
a = requests.adapters.HTTPAdapter(max_retries=20)
sess.mount('https://', a)

If I don't explicitly close the session does it close automatically when my script exits.

The reason I am asking is because if this script is called several thousand times (Each time the previous run is closed/aborted before the next call) will I run into resource problem.

1 Answers

The Session object allows you to reuse the connection across multiple requests. If your Python script ends then the Session is lost, so the connection should be closed. If you want a new connection for each request you can configure keep-alive:

sess = requests.Session()
sess.config['keep_alive'] = False
Related