framework.task¶
task¶
Task¶
- class ding.framework.task.Task[source]¶
Task will manage the execution order of the entire pipeline, register new middleware, and generate new context objects.
- backward(backward_stack: Dict[str, Generator] | None = None) None [source]¶
- Overview:
Execute the rest part of middleware, by the reversed order of registry.
- Arguments:
backward_stack (
Optional[Dict[str, Generator]]
): Replace global backward_stack with a customized stack.
- emit(event: str, *args, only_remote: bool = False, only_local: bool = False, **kwargs) None [source]¶
- Overview:
Emit an event, call listeners.
- Arguments:
event (
str
): Event name.only_remote (
bool
): Only broadcast the event to the connected nodes, default is False.only_local (
bool
): Only emit local event, default is False.args (
any
): Rest arguments for listeners.
- forward(fn: Callable, ctx: Context | None = None) Generator | None [source]¶
- Overview:
This function will execute the middleware until the first yield statment, or the end of the middleware.
- Arguments:
fn (
Callable
): Function with contain the ctx argument in middleware.ctx (
Optional[Context]
): Replace global ctx with a customized ctx.
- Returns:
g (
Optional[Generator]
): The generator if the return value of fn is a generator.
- match_labels(patterns: Iterable[str] | str) bool [source]¶
- Overview:
A list of patterns to match labels.
- Arguments:
patterns (
Union[Iterable[str], str]
): Glob like pattern, e.g. node.1, node.*.
- on(event: str, fn: Callable) None [source]¶
- Overview:
Subscribe to an event, execute this function every time the event is emitted.
- Arguments:
event (
str
): Event name.fn (
Callable
): The function.
- once(event: str, fn: Callable) None [source]¶
- Overview:
Subscribe to an event, execute this function only once when the event is emitted.
- Arguments:
event (
str
): Event name.fn (
Callable
): The function.
- parallel(*fns: List[Callable]) Callable [source]¶
- Overview:
Wrap functions and keep them run in parallel, should not use this funciton in async mode.
- Arguments:
fn (
Callable
): Parallelized middleware, wrap them into one middleware function.
- renew() Task [source]¶
- Overview:
Renew the context instance, this function should be called after backward in the end of iteration.
- run(max_step: int = 1000000000000) None [source]¶
- Overview:
Execute the iterations, when reach the max_step or task.finish is true, The loop will be break.
- Arguments:
max_step (
int
): Max step of iterations.
- serial(*fns: List[Callable]) Callable [source]¶
- Overview:
Wrap functions and keep them run in serial, Usually in order to avoid the confusion of dependencies in async mode.
- Arguments:
fn (
Callable
): Chain a serial of middleware, wrap them into one middleware function.
- use(fn: Callable, lock: bool | allocate_lock = False) Task [source]¶
- Overview:
Register middleware to task. The middleware will be executed by it’s registry order.
- Arguments:
fn (
Callable
): A middleware is a function with only one argument: ctx.lock (
Union[bool, Lock]
): There can only be one middleware execution under lock at any one time.
- Returns:
task (
Task
): The task.
- use_wrapper(fn: Callable) Task [source]¶
- Overview:
Register wrappers to task. A wrapper works like a decorator, but task will apply this decorator on top of each middleware.
- Arguments:
fn (
Callable
): A wrapper is a decorator, so the first argument is a callable function.
- Returns:
task (
Task
): The task.
- wait_for(event: str, timeout: float = inf, ignore_timeout_exception: bool = True) Any [source]¶
- Overview:
Wait for an event and block the thread.
- Arguments:
event (
str
): Event name.timeout (
float
): Timeout in seconds.ignore_timeout_exception (
bool
): If this is False, an exception will occur when meeting timeout.
- wrap(fn: Callable, lock: bool | allocate_lock = False) Callable [source]¶
- Overview:
Wrap the middleware, make it can be called directly in other middleware.
- Arguments:
fn (
Callable
): The middleware.lock (
Union[bool, Lock]
): There can only be one middleware execution under lock at any one time.
- Returns:
- fn_back (
Callable
): It will return a backward function, which will call the rest part of the middleware after yield. If this backward function was not called, the rest part of the middleware will be called in the global backward step.
- fn_back (