API Reference


trio_future.run(nursery: trio.Nursery, async_fn: Callable[[], Awaitable[Any]], *args) → trio_future.future.Future

Run an async function and capture its result in a Future. This is the main entrypoint for trio-future.

Note that this is a synchronous function; it will immediately a Future object. This object can be used to access the return value of async_fn. However, the function will not have begun execution. Under the hood, we will pass the function to nursery.start_soon; its execution will begin when we next defer to the scheduler.

Parameters
  • nursery (trio.Nursery) – Nursery in which to run the function

  • async_fn (Callable[.., Awaitable[Any]]) – A trio-flavored async function to run. Positional arguments may be passed as trailing args, keyword arguments must use functools.partial or similar.

Returns

A Future object allowing access to the return value of async_fn.

Return type

Future


trio_future.gather(nursery: trio.Nursery, futures: List[trio_future.future.Future]) → trio_future.future.Future

Concurrently run multiple Futures.

This function will allow the provided Futures to run concurrently, and gather their results into a single Future containing the results of all the inputs. That is, if each input Future contained an int, then the returned Future will contain a List[int]. In practice, the types need not be homogenous. The list enclosed in the returned Future will have the same ordering as the input list.

If any Futures throw an exception, then the output Future will contain those exceptions, wrapped in a trio.MultiError.

Parameters
  • nursery (trio.Nursery) – Nursery that manages the concurrent execution of the provided futures

  • futures (List[Future]) – Futures to run

Returns

A Future containing the results of all the provided futures.

Return type

Future


class trio_future.Future(result_chan: trio.abc.ReceiveChannel, nursery: trio.Nursery)

Container class for the results of an async function.

async get()

Await the result of this Future and unwrap the value. This will return a plain object if successful. If the bound function raised an exception, this will re-raise it.

Returns

The result of the bound unfunction, unwrapped

Return type

The return type of the bound function.

async outcome() → outcome.Outcome

Await the result of this Future and return it as an instance of outcome.Outcome, which can be one of Value or Error. Can only be called once for this object instance.

Raises

RuntimeError – If this method is called more than once on an object instance.

Returns

Outcome of the function captured by this Future.

Return type

outcome.Outcome