Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many aiohttp sessions for multiple API endpoints?

Suppose I want to periodically (say every 5 secs) issue GET requests to 40 different restful API endpoints using aiohttp, with 40 coroutines in an event loop.

After reading the documentation here https://aiohttp.readthedocs.io/en/stable/client_quickstart.html, I suspect it's best to use 40 aiohttp.ClientSessions, one per endpoint. But I'm not certain that one for the entire app wouldn't be more appropriate. If a single session contains a connection pool, then it sounds well-equipped to deal with 40 endpoints.

Testing performance is non-trivial as the endpoints have some amount of noise/variance. I'm really hoping someone familiar with the library can outline when multiple ClientSessions are necessary.

like image 687
sphere Avatar asked Oct 11 '25 11:10

sphere


1 Answers

You can have as many sessions as you need (40 should definitely be ok). They can be stored in some dict on app level. So after you'll be able to use exact session you need by some key.

Another point, I don't think that sessions are keeping connection at all. It is more like storage for cookies and other HTTP headers, for your future requests.

like image 144
wowkin2 Avatar answered Oct 14 '25 01:10

wowkin2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!