Reputation: 962
The document of Requests give some code like this
s = requests.Session()
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get("http://httpbin.org/cookies")
If I want to use the connection-pooling feature of request.Session, Do I need to instantiate a new Session every time I send a request?, If I do not need a new session, how can I check whether the old seesion is valid now?
Upvotes: 2
Views: 333
Reputation: 15518
If you want to use connection pooling you do not want to create a new session. Connection-pooling is managed by the Session
object, and you should pass all your requests through it.
In general, if you are using Requests to perform more than two or three HTTP requests in your program you should create a single Session
object and run all your requests through it. This will give you all the nice things Requests has to offer, like cookie persistence and connection pooling.
Upvotes: 1
Reputation: 1682
So, I've looked at the documentation and... I think it automatically keeps your session alive for you.
Let me know if you have any problems with dying sessions, but assume that Requests will deal with that for you. I may have misinterpreted the docs, but I don't think you need to worry about it.
From the documentation:
Keep-Alive
Excellent news — thanks to urllib3, keep-alive is 100% automatic within a session! Any requests that you make within a session will automatically reuse the appropriate connection!
Note that connections are only released back to the pool for reuse once all body data has been read; be sure to either set stream to False or read the content property of the Response object.
Upvotes: 0