2020-11-28 13:04:27 +01:00
|
|
|
import giambio
|
|
|
|
from debugger import Debugger
|
|
|
|
|
|
|
|
|
2021-04-22 11:30:35 +02:00
|
|
|
async def child(name: int):
|
|
|
|
print(f"[child {name}] Child spawned!! Sleeping for {name} seconds")
|
2022-09-01 13:18:15 +02:00
|
|
|
try:
|
|
|
|
await giambio.sleep(name)
|
|
|
|
except giambio.exceptions.CancelledError:
|
|
|
|
# Perform some cleanup
|
|
|
|
print(f"[child {name}] I have been cancelled!")
|
|
|
|
raise # Important! Not re-raising the exception *will* break giambio
|
2021-04-22 11:30:35 +02:00
|
|
|
print(f"[child {name}] Had a nice nap!")
|
2020-11-28 13:04:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
start = giambio.clock()
|
|
|
|
async with giambio.create_pool() as pool:
|
2022-09-01 13:18:15 +02:00
|
|
|
await pool.spawn(child, 1) # If you comment this line, the pool will exit immediately!
|
2021-06-08 17:21:59 +02:00
|
|
|
task = await pool.spawn(child, 2)
|
2020-11-28 13:04:27 +01:00
|
|
|
print("[main] Children spawned, awaiting completion")
|
2022-05-14 11:19:55 +02:00
|
|
|
await task.cancel()
|
|
|
|
print("[main] Second child cancelled")
|
2022-09-01 13:18:15 +02:00
|
|
|
# This code always executes, no matter what happens inside the pool (unless an exception
|
|
|
|
# is raised)
|
2021-07-22 11:13:08 +02:00
|
|
|
print(f"[main] Children execution complete in {giambio.clock() - start:.2f} seconds")
|
2020-11-28 13:04:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-08-28 23:26:24 +02:00
|
|
|
giambio.run(main, debugger=())
|