@@ -32,23 +32,15 @@ async def acquire(self) -> None:
3232class ConcurrencyLimiter :
3333 """Caps the number of in-flight operations, with a runtime-adjustable capacity.
3434
35- Unlike :class:`RateLimiter`, which spaces out *when* requests start, this
36- bounds how many run *simultaneously*. It suits APIs that enforce a per-account
37- thread/connection cap (e.g. ScreenScraper) rather than a call rate: because a
38- slot is held for the whole request, slow responses can never cause overlapping
39- requests to exceed the cap.
35+ It suits APIs that enforce a per-account thread/connection cap (e.g. ScreenScraper)
36+ rather than a call rate. Because a slot is held for the whole request, slow
37+ responses can never cause overlapping requests to exceed the cap.
4038
41- The capacity can be raised or lowered at runtime via
42- :meth:`set_max_concurrency` -- for instance once an API response reveals the
43- account's allowance. Use it as an async context manager so the slot is always
44- released, even if the wrapped request raises::
39+ Use it as an async context manager so the slot is always released, even if the
40+ wrapped request raises:
4541
4642 async with limiter:
4743 await do_request()
48-
49- The implementation is loop-agnostic: waiter futures are created against the
50- running loop on acquisition, so a single shared instance works across the
51- distinct event loops used by, e.g., the test suite.
5244 """
5345
5446 def __init__ (self , max_concurrency : int ) -> None :
@@ -76,7 +68,7 @@ def set_max_concurrency(self, max_concurrency: int) -> None:
7668 self ._wake_next ()
7769
7870 async def acquire (self ) -> None :
79- # Re-check on every wake-up: another coroutine may have taken the slot,
71+ # Re-check on every wake-up, as another coroutine may have taken the slot,
8072 # or the capacity may have been lowered while we waited.
8173 while self ._in_flight >= self ._max_concurrency :
8274 loop = asyncio .get_running_loop ()
@@ -88,7 +80,7 @@ async def acquire(self) -> None:
8880 finally :
8981 self ._waiters .remove (waiter )
9082 except asyncio .CancelledError :
91- # We were granted a slot but cancelled before using it; pass the
83+ # We were granted a slot but cancelled before using it, so pass the
9284 # grant on so a waiting peer is not stranded.
9385 if not waiter .cancelled ():
9486 self ._wake_next ()
0 commit comments