API Reference¶
This page documents the public API exposed by AsyncFlow.
Creating a Flow¶
-
class
asyncflow.BaseFlow(spec: Optional[Union[asyncflow.Sequence, asyncflow.Parallel]] = None)¶ Base class for a flow.
There are two APIs available to construct flows. The first is to instantiate a flow with no arguments and use the flow object to decorate functions:
flow = AsyncioFlow() @flow() def f(): ...
The second is to pass a
SeriesorParallelobject to the constructor:dag = Series(Parallel(f, g), h) flow = AsyncioFlow(dag)
- Parameters
spec – optionally provide a
SeriesorParallelobject to specify the flow
-
__call__(upstream: Optional[Union[List[Callable[], Any]], Callable[], Any]]] = None, lock: Optional[Union[asyncflow.Lock, asyncflow.Semaphore]] = None) → Callable[[Callable[], Any]], Callable[], Any]]¶ Add a function to a flow.
BaseFlowobjects can be used as a decorator to add functions to the flow:flow = AsyncioFlow() # inherits from ``BaseFlow`` @flow() def f(): ...
- Parameters
upstream – a function or a list of functions that must complete execution before this function can be executed
lock – a lock that needs to be acquired before this function can run
-
class
asyncflow.AsyncioFlow(spec: Optional[Union[asyncflow.Sequence, asyncflow.Parallel]] = None)¶ Like
BaseFlowbut for the Asyncio runtime.
-
class
asyncflow.CurioFlow(spec: Optional[Union[asyncflow.Sequence, asyncflow.Parallel]] = None)¶ Like
BaseFlowbut for the Curio runtime.
-
class
asyncflow.TrioFlow(spec: Optional[Union[asyncflow.Sequence, asyncflow.Parallel]] = None)¶ Like
BaseFlowbut for the Trio runtime.
Locks¶
-
class
asyncflow.Lock¶ Create a lock.
When provided to a function in a flow, the function will only run when no other function with the lock is running.
Locks can be passed with the decorator based API using the
lockargument,@flow(lock=l) def f(): ...
or the programmatic API using
WithLockflow = Sequence(WithLock(f, l))
Alternative API¶
-
class
asyncflow.Sequence(*args: Union[asyncflow.Sequence, asyncflow.Parallel, asyncflow.WithLock, Callable[], Any]])¶ Define a flow as a succession of operations.
For example, if functions
f,gandhshould be executed sequentially, this can be specified withSequence(f, g, h)
This can be combined with
Parallelto create complex flows programatically.
-
class
asyncflow.Parallel(*args: Union[asyncflow.Sequence, asyncflow.Parallel, asyncflow.WithLock, Callable[], Any]])¶ Define a flow as a set of parallel operations.
For example, if functions
f,gandhcan all run in parallel, this can be specified withParallel(f, g, h)
This can be combined with
Sequenceto create complex flows programatically.
-
class
asyncflow.WithLock(func: Job, lock: LockPlaceholder)¶ Require a lock to be acquired.
This is used to add a lock to a function when the flow is specified with
SequenceorParallel. It is not required when using the decorator-based API.For example, in the following,
flowwill allow up to 2 of the functionsf,gandhto run at a time:from asyncflow import Parallel, WithLock, Semaphore s = Semaphore(2) flow = AsyncioFlow(Parallel( WithLock(f, s), WithLock(g, s), WithLock(h, s) )
- Parameters
func – the function
lock – the lock to associate with
func