2020-11-28 13:04:27 +01:00
|
|
|
import giambio
|
|
|
|
from debugger import Debugger
|
|
|
|
|
|
|
|
|
|
|
|
async def child():
|
|
|
|
print("[child] Child spawned!! Sleeping for 2 seconds")
|
|
|
|
await giambio.sleep(2)
|
|
|
|
print("[child] Had a nice nap!")
|
|
|
|
raise TypeError("rip") # Watch the exception magically propagate!
|
|
|
|
|
|
|
|
|
|
|
|
async def child1():
|
|
|
|
print("[child 1] Child spawned!! Sleeping for 2 seconds")
|
|
|
|
await giambio.sleep(2)
|
|
|
|
print("[child 1] Had a nice nap!")
|
|
|
|
|
|
|
|
|
|
|
|
async def child2():
|
|
|
|
print("[child 2] Child spawned!! Sleeping for 4 seconds")
|
|
|
|
await giambio.sleep(4)
|
|
|
|
print("[child 2] Had a nice nap!")
|
|
|
|
|
|
|
|
|
2020-12-05 17:09:59 +01:00
|
|
|
async def child3():
|
|
|
|
print("[child 3] Child spawned!! Sleeping for 6 seconds")
|
|
|
|
await giambio.sleep(6)
|
|
|
|
print("[child 3] Had a nice nap!")
|
|
|
|
|
|
|
|
|
2020-11-28 13:04:27 +01:00
|
|
|
async def main():
|
|
|
|
start = giambio.clock()
|
|
|
|
try:
|
|
|
|
async with giambio.create_pool() as pool:
|
2021-06-08 17:21:59 +02:00
|
|
|
await pool.spawn(child)
|
|
|
|
await pool.spawn(child1)
|
2020-11-28 13:04:27 +01:00
|
|
|
print("[main] Children spawned, awaiting completion")
|
|
|
|
async with giambio.create_pool() as new_pool:
|
2021-04-22 12:02:40 +02:00
|
|
|
# This pool will be cancelled by the exception
|
|
|
|
# in the other pool
|
2021-06-08 17:21:59 +02:00
|
|
|
await new_pool.spawn(child2)
|
|
|
|
await new_pool.spawn(child3)
|
2020-11-28 13:04:27 +01:00
|
|
|
print("[main] 3rd child spawned")
|
|
|
|
except Exception as error:
|
|
|
|
# Because exceptions just *work*!
|
|
|
|
print(f"[main] Exception from child caught! {repr(error)}")
|
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__":
|
2020-11-29 12:06:09 +01:00
|
|
|
giambio.run(main, debugger=())
|