32 lines
1.4 KiB
Python
32 lines
1.4 KiB
Python
import aiosched
|
|
from raw_wait import child
|
|
|
|
|
|
async def main(children: list[tuple[str, int]]):
|
|
print("[main] Spawning children")
|
|
# Only the first two children will complete
|
|
before = aiosched.clock()
|
|
# This block will not run longer than 5 seconds
|
|
async with aiosched.skip_after(5):
|
|
async with aiosched.create_pool() as pool:
|
|
for name, delay in children:
|
|
await pool.spawn(child, name, delay)
|
|
print("[main] Children spawned")
|
|
# The timeout doesn't apply just to child tasks,
|
|
# but rather to the entire indented block! This
|
|
# means that even things that are awaited instead
|
|
# of spawned will get cancelled when the timeout
|
|
# expires. This only works because we created a
|
|
# task scope that encompasses this whole block!
|
|
await aiosched.sleep(50)
|
|
print("This will never be printed")
|
|
# When using skip_after, no exception is raised when a timeout
|
|
# expires. If you want to handle an exception, you can use with_timeout()
|
|
# instead: when the timeout expires, a TimeoutError exception will be raised
|
|
# instead.
|
|
print(f"[main] Children exited in {aiosched.clock() - before:.2f} seconds")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
aiosched.run(main, [("first", 2), ("second", 4), ("third", 6), ("fourth", 8)], debugger=None)
|