Document multi-producer/consumer channel pattern (#330) - #1141
Open
jbbqqf wants to merge 1 commit into
Open
Conversation
The clone() method on memory object streams is mentioned in passing in the streams guide, but the user needs to read the API doc and infer the multi-producer / multi-consumer shutdown pattern from it. Add a dedicated subsection that walks through the canonical pattern (give every task its own clone, close the originals) and calls out the two well-known footguns: - sharing a stream end across tasks without cloning -> ClosedResourceError - cloning for every task without closing the originals -> consumers hang Inspired by Trio's "Managing multiple producers and/or multiple consumers" section, but written from scratch in anyio terms with a runnable example. Closes agronholm#330.
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.
Changes
Closes #330.
The
clone()method on memory object streams is documented at API level and mentioned in passing indocs/streams.rst, but the canonical multi-producer/multi-consumer shutdown pattern (give every task its own clone, close the originals) is not. Users coming fromtrio.MemoryChannellook for it specifically because Trio's guide has had a dedicated section since 2019.This PR adds a
Multiple producers and/or multiple consumerssubsection underMemory object streamsthat:clone()andasync withfor tear-downClosedResourceErrorInspired by Trio's Managing multiple producers and/or multiple consumers section, but the prose and example are written from scratch in anyio terms (
create_memory_object_stream,MemoryObjectSendStream,create_task_group) so there is no copyright concern.The example was run as-is against current
master; output is the expected six lines plus a clean exit (the order varies because tasks race, but the program terminates without exceptions).Checklist
tests/) which would fail without your patchdocs/), in case of behavior changes or new featuresdocs/versionhistory.rst).This is a documentation-only change so no test was added, but the example was executed end-to-end. Sphinx strict build (
sphinx-build -W --keep-going -b html . _build/html) was clean.Reproduce BEFORE/AFTER yourself (copy-paste)
Risk / blast radius
Documentation-only.
PR drafted with assistance from Claude (Anthropic). The example was executed end-to-end and the docs build was checked in strict mode.