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()
|
2023-06-19 16:33:58 +02:00
|
|
|
with structio.skip_after(i):
|
2023-05-17 00:27:24 +02:00
|
|
|
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:
|
|
|
|
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:
|
2023-06-19 16:33:58 +02:00
|
|
|
s = structio.TaskScope(shielded=True)
|
|
|
|
task = p.spawn(canceller, s, i)
|
2023-05-17 11:16:58 +02:00
|
|
|
await structio.sleep(j)
|
2023-06-19 16:33:58 +02:00
|
|
|
assert not task.done()
|
2023-05-17 11:16:58 +02:00
|
|
|
print("[main] Canceling scope")
|
2023-06-01 20:33:09 +02:00
|
|
|
# Shields only protect from indirect cancellations
|
|
|
|
# coming from outer scopes: they are still cancellable
|
|
|
|
# explicitly!
|
2023-05-17 11:16:58 +02:00
|
|
|
s.cancel()
|
|
|
|
print(f"[main] Exited in {structio.clock() - t:.2f} seconds")
|
|
|
|
|
|
|
|
|
2023-06-19 16:33:58 +02:00
|
|
|
structio.run(main, 2)
|
|
|
|
structio.run(main_cancel, 5, 2)
|