Consider the following code:
import trio
async def broken_fn():
await trio.sleep(4)
print(1 / 0)
async def worker_fn():
while True:
print("working...")
await trio.sleep(1)
async def main():
async with trio.open_nursery() as nursery:
nursery.start_soon(broken_fn)
nursery.start_soon(worker_fn)
trio.run(main)
How do I prevent the exception raised by broken_fn
to abort its siblings in the nursery without touching the definition of broken_fn
? Is the following the best way?
async def main():
async def supress(fn):
try:
await fn()
except Exception as e:
print(f"caught {e}!")
async with trio.open_nursery() as nursery:
nursery.start_soon(supress, broken_fn)
nursery.start_soon(worker_fn)
Do I have any other options?