mirror of https://github.com/nocturn9x/giambio.git
37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
from debugger import Debugger
|
|
import giambio
|
|
|
|
|
|
# A test for events
|
|
|
|
|
|
async def child(ev: giambio.Event, pause: int):
|
|
print("[child] Child is alive! Going to wait until notified")
|
|
start_total = giambio.clock()
|
|
data = await ev.wait()
|
|
end_pause = giambio.clock() - start_total
|
|
print(f"[child] Parent set the event with value {data}, exiting in {pause} seconds")
|
|
start_sleep = giambio.clock()
|
|
await giambio.sleep(pause)
|
|
end_sleep = giambio.clock() - start_sleep
|
|
end_total = giambio.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 giambio.create_pool() as pool:
|
|
event = giambio.Event()
|
|
print("[parent] Spawning child task")
|
|
await pool.spawn(child, event, pause + 2)
|
|
start = giambio.clock()
|
|
print(f"[parent] Sleeping {pause} second(s) before setting the event")
|
|
await giambio.sleep(pause)
|
|
await event.trigger(True)
|
|
print("[parent] Event set, awaiting child completion")
|
|
end = giambio.clock() - start
|
|
print(f"[parent] Child exited in {end:.2f} seconds")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
giambio.run(parent, 3, debugger=())
|