user4385532
user4385532

Reputation:

Does limiting the maximum number of concurrent connections also limit the number of concurrent requests?

This Web tutorial and also this SO answer recommend the use of semaphores to limit the number of concurrent requests made with aiohttp.

I am confused because aiohttp provides on its own a facility to limit the number of concurrent connections (limit and limit_per_host, documented here) - so is using semaphores not reinventing the wheel?

Perhaps it's not. Can there be multiple concurrent requests per connection? It appears there can, as per this Wikipedia article and also this SO answer. So perhaps setting limit and/or limit_per_host in aiohttp, which as per the docs I linked to limits concurrent connections, does not have the effect of limiting concurrent requests.

I'm still confused because if this is the case, what's the use of these parameters aiohttp provides? Why would a user want to limit connections rather than requests? But such reasoning of course doesn't entail anything so I was ready to move on and use semaphores.

Then I stumbled upon this SO question. It has two relatively highly upvoted answers. One of these answers again recommends using semaphores. But the other answer recommends making use of the aiohttp facilities limit and limit_per_host. If this answer is correct then limiting connections in aiohttp also limits requests - so no semaphores are necessary (unless one also wishes to limit the rate of requests per second, which is not what I'm tackling here)

And this is what I'd like to ask in this question. Does limiting concurrent connections in aiohttp via limit and/or limit_per_host also limit concurrent requests? I suppose that the answer depends on whether aiohttp does or does not use only one connection per request, which I don't know either.

Does aiohttp only use one connection per request? Does limiting concurrent connections also limit concurrent requests?

Upvotes: 5

Views: 1926

Answers (2)

Mikhail Gerasimov
Mikhail Gerasimov

Reputation: 39546

Does aiohttp only use one connection per request?

By default if you make several requests to single host using same ClientSession instance, aiohttp can reuse existing connection(s): By default all connectors support keep-alive connections (source)

You can share connection between different sessions using connector_owner=False (source).

But making single request is possible only through single connection if that's what you're asking about.


Does limiting concurrent connections also limit concurrent requests?

It does (for example, see plot here), but you shouldn't rely on it anyway.

"Connection limit" is a low-level detail of aiohttp's request implementation. You don't really want to work with low-level detail, what you want is to work with high-level abstraction "limit number of concurrent requests". And that's what semaphore can be used for.

To better understand the idea consider following situation: your script makes concurrent requests using aiohttp and some another http client library same time. Relying on aiohttp's limit won't affect other library. Global semaphore on the other hand will allow to limit all requests regardless of how they are implemented.

Upvotes: 2

user4815162342
user4815162342

Reputation: 154996

The wikipedia article refers to persistent connections, i.e. re-using the same TCP connection for multiple requests, the requests still being serial rather than concurrent. HTTP/2 does allow multiplexing concurrent requests on a single TCP connection, but aiohttp doesn't implement it.

That makes the two limits equivalent: limiting the number of parallel connections effectively limits the number of concurrent requests. As Mikhail points out, this can be considered an implementation detail.

Upvotes: 2

Related Questions