Debian Codegraph @

docs/source/reference-core/channels-simple.py

raw
1 import trio
2
3
4 async def main():
5 async with trio.open_nursery() as nursery:
6 # Open a channel:
7 send_channel, receive_channel = trio.open_memory_channel(0)
8 # Start a producer and a consumer, passing one end of the channel to
9 # each of them:
10 nursery.start_soon(producer, send_channel)
11 nursery.start_soon(consumer, receive_channel)
12
13
14 async def producer(send_channel):
15 # Producer sends 3 messages
16 for i in range(3):
17 # The producer sends using 'await send_channel.send(...)'
18 await send_channel.send(f"message {i}")
19
20
21 async def consumer(receive_channel):
22 # The consumer uses an 'async for' loop to receive the values:
23 async for value in receive_channel:
24 print(f"got value {value!r}")
25
26
27 trio.run(main)