This repository has been archived on 2023-05-12. You can view files and clone it, but cannot push or open issues or pull requests.
aiosched/tests/events.py

36 lines
1.2 KiB
Python

import aiosched
async def child(ev: aiosched.Event, pause: int):
print("[child] Child is alive! Going to wait until notified")
start_total = aiosched.clock()
await ev.wait()
end_pause = aiosched.clock() - start_total
print(f"[child] Parent set the event, exiting in {pause} seconds")
start_sleep = aiosched.clock()
await aiosched.sleep(pause)
end_sleep = aiosched.clock() - start_sleep
end_total = aiosched.clock() - start_total
print(
f"[child] Done! Slept for {end_total:.2f} seconds total ({end_pause:.2f} waiting, {end_sleep:.2f} sleeping), nice nap!"
)
async def parent(pause: int = 1):
async with aiosched.with_context() as ctx:
event = aiosched.Event()
print("[parent] Spawning child task")
await ctx.spawn(child, event, pause + 2)
start = aiosched.clock()
print(f"[parent] Sleeping {pause} second(s) before setting the event")
await aiosched.sleep(pause)
await event.trigger()
print("[parent] Event set, awaiting child completion")
end = aiosched.clock() - start
print(f"[parent] Child exited in {end:.2f} seconds")
if __name__ == "__main__":
aiosched.run(parent, 3, debugger=None)