36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
from debugger import Debugger
|
|
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)
|