Add asyncio memory object channels (open_channel)#145356
Open
kovan wants to merge 1 commit intopython:mainfrom
Open
Add asyncio memory object channels (open_channel)#145356kovan wants to merge 1 commit intopython:mainfrom
kovan wants to merge 1 commit intopython:mainfrom
Conversation
Introduces open_channel() returning (SendChannel, ReceiveChannel) — a bounded, split-ownership channel primitive for asyncio. Features include ref-counted clone() for fan-in/fan-out patterns, backpressure via max_buffer_size, deterministic close signaling (EndOfChannel when all senders close, BrokenResourceError when all receivers close), and async iteration support. New exceptions: EndOfChannel, ClosedResourceError, BrokenResourceError, WouldBlock. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
Adds
asyncio.open_channel(max_buffer_size)— a split send/receive channel primitive that improves onasyncio.Queuewith:max_buffer_size(supports0for rendezvous,math.inffor unbounded)SendChannel/ReceiveChannelendpoints enforce correct resource managementclone()enables multi-producer / multi-consumer (fan-in / fan-out) with clean shutdownasync for item in receive_channelEndOfChannelwhen all senders close,BrokenResourceErrorwhen all receivers closewith) and async (async with)Part of the proposal to adopt proven anyio/Trio patterns natively into asyncio (Tier 2, item 2.2).
API
New public names
asyncio.open_channel()— factory returning(SendChannel, ReceiveChannel)asyncio.SendChannel— send endpoint withsend(),send_nowait(),clone(),close()asyncio.ReceiveChannel— receive endpoint withreceive(),receive_nowait(),clone(), async iterationasyncio.ChannelStatistics— frozen dataclass for introspectionasyncio.EndOfChannel— raised when all senders are closedasyncio.ClosedResourceError— raised when using a closed endpointasyncio.BrokenResourceError— raised when sending with no open receiversasyncio.WouldBlock— raised by_nowaitmethods when operation would blockFiles
Lib/asyncio/channels.pyLib/asyncio/exceptions.pyEndOfChannel,ClosedResourceError,BrokenResourceError,WouldBlockLib/asyncio/__init__.pychannelsmoduleLib/test/test_asyncio/test_channels.pyTest plan
./python -m test test_asyncio.test_channels -v— all 40 tests pass🤖 Generated with Claude Code