Add asyncio.CapacityLimiter with borrower tracking and dynamic capacity#145354
Closed
kovan wants to merge 1 commit intopython:mainfrom
Closed
Add asyncio.CapacityLimiter with borrower tracking and dynamic capacity#145354kovan wants to merge 1 commit intopython:mainfrom
kovan wants to merge 1 commit intopython:mainfrom
Conversation
CapacityLimiter is a smarter concurrency limiter than Semaphore: - Borrower tracking prevents same-task reacquisition (deadlock prevention) - Dynamic total_tokens allows runtime capacity changes - on_behalf_of API enables acquiring/releasing for arbitrary objects - Statistics introspection via CapacityLimiterStatistics dataclass - WouldBlock exception for nowait acquire variants - FIFO fairness via OrderedDict-based waiter queue Part of the effort to adopt proven anyio/Trio patterns into asyncio. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Member
|
See my comment. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add
asyncio.CapacityLimiter, a smarter concurrency limiter thanSemaphore, adopting a proven pattern from Trio/anyio:total_tokens— capacity can be changed at runtime; increasing wakes waiterson_behalf_ofAPI — acquire/release on behalf of arbitrary objects (enables thread integration)CapacityLimiterStatisticsdataclassWouldBlockexception — raised bynowaitvariants when a token isn't immediately availableOrderedDict-based waiter queue ensures fair schedulingPure Python implementation, no C code needed.
Part of the proposal to adopt proven anyio/Trio patterns natively into asyncio (Tier 2, item 2.1).
API
Test plan
acquire_nowaitfast path andWouldBlockwhen at capacityRuntimeErrorRuntimeErroracquirecleans up correctlytotal_tokensincrease wakes waiterstotal_tokensdecrease blocks new acquires without evictingtotal_tokenson_behalf_ofacquire/release with explicit borrowersstatistics()reflects correct statereproutputmath.infcapacity edge casestest_asyncio.test_lockstests still pass🤖 Generated with Claude Code