structio/tests/ki_test.py

35 lines
942 B
Python

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
print(
f"Oh no, I've been cancelled! (was gonna sleep {n - slept:.2f} more seconds)"
)
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")