Created
February 28, 2026 06:49
-
-
Save graingert/9dc56c649cef82c4cc21a5c24b8d0dcf to your computer and use it in GitHub Desktop.
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
| @app.get("/news-and-weather") | |
| @contextlib.asynccontextmanager | |
| async def news_and_weather() -> AsyncGenerator[MemoryObjectRecieveStream[bytes]]: | |
| async with anyio.create_task_group() as tg: | |
| tx, rx = anyio.create_memory_object_stream[bytes]() | |
| with tx, rx: | |
| tg.start_soon(ws_stream, "ws://example.com/news", tx.clone()) | |
| tg.start_soon(ws_stream, "ws://example.com/weather", tx.clone()) | |
| tx.close() | |
| yield rx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just for completeness, in case anyone else comes here checking for this discussion that we finished on chat, here's the version that should work fine.
The trick is that the task group is created in a dependency with yield, that is converted by FastAPI to an async context manager, and the exit code is run after the response is done, so nothing ever "yields out" of the task group: