i'm using an event to trigger the cancellation of a nested scope, but it's not behaving as i would expect. Looks something like this:
cancel_event = Event()
async with create_task_group() as tg:
tg.start_soon(long_running_task, cancel_event)
def long_running_task(cancel_event):
try:
async with create_task_group() as tg:
tg.start_soon(some_task)
await cancel_event.wait()
tg.cancel_scope.cancel()
except get_cancelled_exc_class():
# cleanup stuff...
raise
set()
-ing the event triggerr the cancellation, but the exception never catches anything and the cleanup logic doesn't run. am I missing something?
from anyio import Event, create_task_group, sleep, get_cancelled_exc_class, run
async def task(cancel_event: Event):
async def long_task():
while True:
await sleep(1)
async with create_task_group() as tg:
try:
tg.start_soon(long_task)
await cancel_event.wait()
tg.cancel_scope.cancel()
except get_cancelled_exc_class():
print("Never printed")
raise
async def main():
cancel_event = Event()
async with create_task_group() as tg:
tg.start_soon(task, cancel_event)
await sleep(2)
cancel_event.set()
run(main)
async with reporter() as r:
async def some_thing()
try:
await foo()
except BaseException as e:
r.report(e)
...
async with report_crashes(): ...
which captures and reports on a wide range of errors. It's actually outside the our cancel scope so we don't normally need shielding but we don't have control over how the user is running their event loop so there may be a cancel scope outside of our code and we need to report that failure still.
move_on_after(..., shield=True):