如何防止 Child 关闭整个托儿所引发的异常

How Can I Prevent an Exception Raised By a Child Taking Down the Whole Nursery

考虑以下代码:

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)

如何在不触及 broken_fn 的定义的情况下防止 broken_fn 引发异常以中止其在托儿所中的兄弟姐妹?以下是最好的方法吗?

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)

我还有其他选择吗?

start_soon() 中没有忽略异常的机制,如果您正在寻找的话。

您也可以以通用方式执行此操作,以免为每个单独的函数构建包装器:

from contextlib import suppress
from functools import wraps
from typing import Callable

def suppressed(func: Callable, *exceptions: BaseException):
    @wraps(func)
    async def wrapper(*args, **kwargs):
        with suppress(*exceptions):
            return await func(*args, **kwargs)
        
    return wrapper

然后:

async with trio.open_nursery() as nursery:
    nursery.start_soon(suppressed(broken_fn, Exception))
    nursery.start_soon(worker_fn)