Reusing client session in asyncio library

Hello everyone currently I am working on async Jenkins library (

So is it good idea to create client session once in class constructor or using new session for every API endpoint is pretty enough?

And as I understand I should create client session in async setup and add task which will close session when destroying my class instance.

aiohttp’s ClientSession is meant to be persist for a long time (e.g., lifetime of the entire application) so that its users could benefit from connection pooling.
But as far as I know, the overhead for setting up and trearing down ClientSession objects are not that considerably big.
So it is up to your design, whether to reuse ClientSession across multiple API calls.

If I were you, I would introduce the same “API session” concept where the lifetime of an internal aiohttp.ClientSession is synchronized to its lifetime, and let the library users control the API session lifecycles.

1 Like

Thanks. So did you see good example of it?