Coroutines and loops¤
Create a loop with tinyio.Loop(). It has a single method, .run(coro), which consumes a coroutine, and which returns the output of that coroutine.
Each coroutine is just a Python generator using yield. Coroutines can yield four possible things:
yield: yield nothing, this just pauses and gives other coroutines a chance to run.yield coro: wait on a single coroutine, in which case we'll resume with the output of that coroutine once it is available.yield [coro1, coro2, coro3]: wait on multiple coroutines by putting them in a list, and resume with a list of outputs once all have completed. This is whatasynciocalls a 'gather' or 'TaskGroup', and whattriocalls a 'nursery'.yield {coro1, coro2, coro3}: schedule one or more coroutines but do not wait on their result — they will run independently in the background.
If you yield on the same coroutine multiple times (e.g. in a diamond dependency pattern) then the coroutine will be scheduled once, and on completion all dependees will receive its output. (You can even do this if the coroutine has already finished: yield on it to retrieve its output.)
tinyio.Loop
¤
Event loop for running tinyio-style coroutines. The results of each coroutine are recorded by the loop, so you
can call tinyio.Loop.run multiple times.
Unlike other asynchronous frameworks, loops are not restricted to one-per-thread.
__init__()
¤
Initializes the loop.
run(coro: Coro[~_Return], exception_group: None | bool = None) -> ~_Return
¤
Run the specified coroutine in the event loop.
Arguments:
coro: a Python coroutine to run; it may yieldNone, other coroutines, or lists-of-coroutines, or sets-of-coroutines.exception_group: in the event of an error in one of the coroutines, then all other coroutines will be cancelled and the loop will be shut down the loop. This arguments determines the kind of exception raised out of the loop:- if
Falsethen raise just that error, silently ignoring any errors that occur when cancelling the other coroutines. - if
Truethen always raise a{Base}ExceptionGroup, whose first sub-exception will be the original error, and whose later sub-exceptions will be any errors that occur whilst cancelling the other coroutines. (Including all thetinyio.CancelledErrors that indicate successful cancellation.) - if
None(the default) then raise just the original error if all other coroutines shut down successfully, and raise a{Base}ExceptionGroupif any other coroutine raises an exception during shutdown. (Excluding all thetinyio.CancelledErrors that indicate successful cancellation.)
- if
Returns:
The value returned from coro.