2023-05-17 00:27:24 +02:00
|
|
|
import structio
|
|
|
|
|
|
|
|
|
|
|
|
async def shielded(i):
|
|
|
|
print("[shielded] Entering shielded section")
|
|
|
|
with structio.TaskScope(shielded=True) as s:
|
|
|
|
await structio.sleep(i)
|
|
|
|
print(f"[shielded] Slept {i} seconds")
|
|
|
|
s.shielded = False
|
|
|
|
print(f"[shielded] Exited shielded section, sleeping {i} more seconds")
|
|
|
|
await structio.sleep(i)
|
|
|
|
|
|
|
|
|
|
|
|
async def main(i):
|
|
|
|
print(f"[main] Parent has started, finishing in {i} seconds")
|
|
|
|
t = structio.clock()
|
|
|
|
with structio.skip_after(1):
|
|
|
|
await shielded(i)
|
|
|
|
print(f"[main] Exited in {structio.clock() - t:.2f} seconds")
|
|
|
|
|
|
|
|
|
2023-05-17 11:16:58 +02:00
|
|
|
async def canceller(s, i):
|
|
|
|
print("[canceller] Entering shielded section")
|
|
|
|
with s:
|
|
|
|
s.shielded = True
|
|
|
|
await structio.sleep(i)
|
|
|
|
|
|
|
|
|
|
|
|
async def main_cancel(i, j):
|
|
|
|
print(f"[main] Parent has started, finishing in {j} seconds")
|
|
|
|
t = structio.clock()
|
|
|
|
async with structio.create_pool() as p:
|
|
|
|
s = structio.TaskScope()
|
|
|
|
p.spawn(canceller, s, i)
|
|
|
|
await structio.sleep(j)
|
|
|
|
print("[main] Canceling scope")
|
|
|
|
s.cancel()
|
|
|
|
print(f"[main] Exited in {structio.clock() - t:.2f} seconds")
|
|
|
|
|
|
|
|
|
2023-05-17 00:27:24 +02:00
|
|
|
structio.run(main, 5)
|
2023-05-17 11:16:58 +02:00
|
|
|
structio.run(main_cancel, 5, 3)
|