2023-05-16 15:48:19 +02:00
|
|
|
import structio
|
|
|
|
import random
|
|
|
|
|
|
|
|
|
|
|
|
async def waiter(ch: structio.ChannelReader):
|
|
|
|
print("[waiter] Waiter is alive!")
|
2023-05-17 16:01:22 +02:00
|
|
|
while True:
|
|
|
|
print("[waiter] Awaiting events")
|
|
|
|
try:
|
2023-05-16 15:48:19 +02:00
|
|
|
evt: structio.Event = await ch.receive()
|
2023-05-17 16:01:22 +02:00
|
|
|
except structio.ResourceClosed:
|
|
|
|
break
|
|
|
|
print("[waiter] Received event, waiting to be triggered")
|
|
|
|
t = structio.clock()
|
|
|
|
await evt.wait()
|
|
|
|
print(f"[waiter] Event triggered after {structio.clock() - t:.2f} seconds")
|
2023-05-16 15:48:19 +02:00
|
|
|
print("[waiter] Done!")
|
|
|
|
|
|
|
|
|
2023-05-17 16:01:22 +02:00
|
|
|
async def sender(ch: structio.Channel, n: int):
|
2023-05-16 15:48:19 +02:00
|
|
|
print("[sender] Sender is alive!")
|
|
|
|
async with ch:
|
2023-05-17 16:01:22 +02:00
|
|
|
# Channel is automatically closed when exiting
|
|
|
|
# the async with block
|
2023-05-16 15:48:19 +02:00
|
|
|
for _ in range(n):
|
|
|
|
print("[sender] Sending event")
|
|
|
|
ev = structio.Event()
|
|
|
|
await ch.send(ev)
|
|
|
|
t = random.random()
|
|
|
|
print(f"[sender] Sent event, sleeping {t:.2f} seconds")
|
|
|
|
await structio.sleep(t)
|
|
|
|
print("[sender] Setting the event")
|
|
|
|
ev.set()
|
|
|
|
print("[sender] Done!")
|
|
|
|
|
|
|
|
|
|
|
|
async def main(n: int):
|
|
|
|
print("[main] Parent is alive")
|
|
|
|
channel = structio.MemoryChannel(1)
|
|
|
|
async with structio.create_pool() as pool:
|
2023-05-17 16:01:22 +02:00
|
|
|
# Each end of the channel can be used independently,
|
|
|
|
# and closing one does not also close the other (which
|
|
|
|
# is why we pass the full channel object to our sender
|
|
|
|
# so it can close both ends and cause the reader to catch
|
|
|
|
# the closing exception and exit cleanly)
|
2023-05-16 15:48:19 +02:00
|
|
|
pool.spawn(waiter, channel.reader)
|
2023-05-17 16:01:22 +02:00
|
|
|
pool.spawn(sender, channel, n)
|
2023-05-16 15:48:19 +02:00
|
|
|
print("[main] Children spawned")
|
|
|
|
print("[main] Done!")
|
|
|
|
|
|
|
|
|
|
|
|
structio.run(main, 3)
|