运行 Trio

Entering Trio

如果你想使用 Trio,那么你要做的第一件事就是调用 trio.run():

If you want to use Trio, then the first thing you have to do is call trio.run():

trio.run(async_fn, *args, clock=None, instruments=(), restrict_keyboard_interrupt_to_checkpoints=False, strict_exception_groups=True)

Run a Trio-flavored async function, and return the result.

Calling:

run(async_fn, *args)

is the equivalent of:

await async_fn(*args)

except that run() can (and must) be called from a synchronous context.

This is Trio's main entry point. Almost every other function in Trio requires that you be inside a call to run().

参数:
  • async_fn (Callable[[Unpack[PosArgT]], Awaitable[RetT]]) -- An async function.

  • args (Unpack[PosArgT]) -- Positional arguments to be passed to async_fn. If you need to pass keyword arguments, then use functools.partial().

  • clock (Clock | None) -- None to use the default system-specific monotonic clock; otherwise, an object implementing the trio.abc.Clock interface, like (for example) a trio.testing.MockClock instance.

  • instruments (list of trio.abc.Instrument objects) -- Any instrumentation you want to apply to this run. This can also be modified during the run; see 工具 API.

  • restrict_keyboard_interrupt_to_checkpoints (bool) --

    What happens if the user hits control-C while run() is running? If this argument is False (the default), then you get the standard Python behavior: a KeyboardInterrupt exception will immediately interrupt whatever task is running (or if no task is running, then Trio will wake up a task to be interrupted). Alternatively, if you set this argument to True, then KeyboardInterrupt delivery will be delayed: it will be only be raised at checkpoints, like a Cancelled exception.

    The default behavior is nice because it means that even if you accidentally write an infinite loop that never executes any checkpoints, then you can still break out of it using control-C. The alternative behavior is nice if you're paranoid about a KeyboardInterrupt at just the wrong place leaving your program in an inconsistent state, because it means that you only have to worry about KeyboardInterrupt at the exact same places where you already have to worry about Cancelled.

    This setting has no effect if your program has registered a custom SIGINT handler, or if run() is called from anywhere but the main thread (this is a Python limitation), or if you use open_signal_receiver() to catch SIGINT.

  • strict_exception_groups (bool) -- Unless set to False, nurseries will always wrap even a single raised exception in an exception group. This can be overridden on the level of individual nurseries. Setting it to False will be deprecated and ultimately removed in a future version of Trio.

返回类型:

RetT

返回:

Whatever async_fn returns.

抛出:
  • TrioInternalError -- if an unexpected error is encountered inside Trio's internal machinery. This is a bug and you should let us know.

  • Anything else -- if async_fn raises an exception, then run() propagates it.