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