@@ -25,17 +25,34 @@ class Pipe(typing.Generic[ChannelMessageT]):
2525
2626 Example:
2727 ```python
28- from frequenz.channels import Broadcast, Pipe
29-
30- channel1: Broadcast[int] = Broadcast(name="channel1")
31- channel2: Broadcast[int] = Broadcast(name="channel2")
32-
33- receiver_chan1 = channel1.new_receiver()
34- sender_chan2 = channel2.new_sender()
35-
36- async with Pipe(channel2.new_receiver(), channel1.new_sender()):
37- await sender_chan2.send(10)
38- assert await receiver_chan1.receive() == 10
28+ import asyncio
29+ from contextlib import closing, aclosing, AsyncExitStack
30+
31+ from frequenz.channels import Broadcast, Pipe, Receiver
32+
33+ async def main() -> None:
34+ # Channels, receivers and Pipe are in AsyncExitStack
35+ # to close and stop them at the end.
36+ async with AsyncExitStack() as stack:
37+ source_channel = await stack.enter_async_context(
38+ aclosing(Broadcast[int](name="source channel"))
39+ )
40+ source_receiver = stack.enter_context(closing(source_channel.new_receiver()))
41+
42+ forwarding_channel = await stack.enter_async_context(
43+ aclosing(Broadcast[int](name="forwarding channel"))
44+ )
45+ await stack.enter_async_context(
46+ Pipe(source_receiver, forwarding_channel.new_sender())
47+ )
48+
49+ receiver = stack.enter_context(closing(forwarding_channel.new_receiver()))
50+
51+ source_sender = source_channel.new_sender()
52+ await source_sender.send(10)
53+ assert await receiver.receive() == 11
54+
55+ asyncio.run(main())
3956 ```
4057 """
4158
@@ -59,8 +76,8 @@ async def __aenter__(self) -> Pipe[ChannelMessageT]:
5976
6077 async def __aexit__ (
6178 self ,
62- _exc_type : typing .Type [BaseException ],
63- _exc : BaseException ,
79+ _exc_type : typing .Type [BaseException ] | None ,
80+ _exc : BaseException | None ,
6481 _tb : typing .Any ,
6582 ) -> None :
6683 """Exit the runtime context."""
0 commit comments