Ahtesham Akhtar
Ahtesham Akhtar

Reputation: 303

Will a request.Session close automatically when my script exits

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.

Upvotes: 3

Views: 1320

Answers (1)

andrew
andrew

Reputation: 5559

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

Upvotes: 2

Related Questions