2023-05-15 18:25:02 +02:00
|
|
|
import structio
|
|
|
|
|
|
|
|
|
|
|
|
async def child(n: int):
|
|
|
|
print(f"Going to sleep for {n} seconds!")
|
|
|
|
i = structio.clock()
|
|
|
|
try:
|
|
|
|
await structio.sleep(n)
|
|
|
|
except structio.Cancelled:
|
|
|
|
slept = structio.clock() - i
|
2023-06-19 17:34:44 +02:00
|
|
|
print(
|
|
|
|
f"Oh no, I've been cancelled! (was gonna sleep {n - slept:.2f} more seconds)"
|
|
|
|
)
|
2023-05-15 18:25:02 +02:00
|
|
|
raise
|
|
|
|
print(f"Slept for {structio.clock() - i:.2f} seconds!")
|
|
|
|
|
|
|
|
|
|
|
|
async def main() -> int:
|
|
|
|
print("Parent is alive. Spawning children")
|
|
|
|
t = structio.clock()
|
|
|
|
try:
|
|
|
|
async with structio.create_pool() as pool:
|
|
|
|
pool.spawn(child, 5)
|
|
|
|
pool.spawn(child, 3)
|
|
|
|
pool.spawn(child, 8)
|
|
|
|
print(f"Children spawned, awaiting completion")
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("Ctrl+C caught")
|
|
|
|
print(f"Children have completed in {structio.clock() - t:.2f} seconds")
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
assert structio.run(main) == 0
|
|
|
|
print("Execution complete")
|