Task¶
A Task defines the implementation of a given operation, the inputs required and the scheduling metadata. Usually, a Task is not created directly, but rather through the use of the @task decorator.
Example¶
A task function is decorated via the @task decorator and must accept the TaskRun object as its first and only argument.
from fluid.scheduler import task, TaskRun
@task
async def hello(ctx: TaskRun) -> None:
print("Hello, world!")
For retry configuration (retry, rate_limit_retry) see Task Retry.
fluid.scheduler.task
¶
task(executor: TaskExecutor) -> Task
task(
*,
name: str | None = None,
schedule: Scheduler | None = None,
short_description: str | None = None,
description: str | None = None,
randomize: RandomizeType | None = None,
max_concurrency: int | None = None,
priority: TaskPriority | None = None,
cpu_bound: bool | None = None,
k8s_config: K8sConfig | None = None,
timeout_seconds: int | None = None,
tags: Sequence[str] | None = None,
retry: RetryPolicy | None = None,
rate_limit_retry: RetryPolicy | None = None,
env: dict[str, str] | None = None
) -> TaskConstructor
task(
executor=None,
*,
name=None,
schedule=None,
short_description=None,
description=None,
randomize=None,
max_concurrency=None,
priority=None,
cpu_bound=None,
k8s_config=None,
timeout_seconds=None,
tags=None,
retry=None,
rate_limit_retry=None,
env=None
)
Decorator to create a Task from a function and optional parameters.
This decorator can be used in two ways:
- As a simple decorator of the executor function
- As a function with keyword arguments for greater control over the task configuration
| PARAMETER | DESCRIPTION |
|---|---|
executor
|
The executor function for the task
TYPE:
|
name
|
The name of the task. If None, the name will be derived from the executor function
TYPE:
|
schedule
|
The schedule for the tas. If None, the task will not be scheduled
TYPE:
|
short_description
|
A short description of the task. If not provided it will be extracted from the task function docstring first line
TYPE:
|
description
|
A detailed description of the task. If not provided it will be extracted from the task function docstring
TYPE:
|
randomize
|
Randomization settings for the task
TYPE:
|
max_concurrency
|
The maximum number of concurrent executions of the task
TYPE:
|
priority
|
The priority of the task such as high, medium, low
TYPE:
|
cpu_bound
|
Whether the task is CPU bound
TYPE:
|
k8s_config
|
Kubernetes configuration - None means use the default configuration
TYPE:
|
timeout_seconds
|
Task timeout in seconds - how long the task can run before being aborted
TYPE:
|
tags
|
Task tags - used for categorization and filtering of tasks
TYPE:
|
retry
|
Retry policy for execution failures
TYPE:
|
rate_limit_retry
|
Retry policy when the task is rate limited by max_concurrency
TYPE:
|
env
|
Extra environment variables injected into the subprocess or k8s job
TYPE:
|
Source code in fluid/scheduler/models.py
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 | |
fluid.scheduler.Task
¶
Bases: NamedTuple, Generic[TP]
A Task configuration.
This is not created directly, but rather through the use of the @task decorator.
Executes any time it is invoked
cpu_executor
class-attribute
instance-attribute
¶
Function of a cpu bound task, run by the process executing it.
A cpu bound task has the subprocess runner as its executor and the
function it was declared with here, so the process which executes the task
can call it directly instead of spawning another one for it.
short_description
class-attribute
instance-attribute
¶
Short task description - one line
description
class-attribute
instance-attribute
¶
Task description - obtained from the executor docstring if not provided
schedule
class-attribute
instance-attribute
¶
Task schedule - None means the task is not scheduled
randomize
class-attribute
instance-attribute
¶
Randomize function for task schedule
max_concurrency
class-attribute
instance-attribute
¶
how many tasks can be run concurrently - 0 means no limit
timeout_seconds
class-attribute
instance-attribute
¶
Task timeout in seconds - how long the task can run before being aborted
priority
class-attribute
instance-attribute
¶
priority = TaskPriority.medium
Task priority - high, medium, low
k8s_config
class-attribute
instance-attribute
¶
Kubernetes configuration for tasks run on Kubernetes cluster.
tags
class-attribute
instance-attribute
¶
Task tags - used for categorization and filtering of tasks
retry
class-attribute
instance-attribute
¶
Retry policy for general execution failures.
rate_limit_retry
class-attribute
instance-attribute
¶
Retry policy when the executor raises RateLimitError.
env
class-attribute
instance-attribute
¶
Extra environment variables injected into the subprocess or k8s job.
run_executor
¶
The function which runs the task.
A cpu bound task is executed by the exec command of the task manager
client, which runs it to completion in its own process. in_process
is true there, and the function the task was declared with is called
directly. Anywhere else executor is called, and for a cpu bound task
that is the runner which spawns the exec command.
Source code in fluid/scheduler/models.py
get_k8s_config
¶
info
¶
Return task info object
Source code in fluid/scheduler/models.py
fluid.scheduler.TaskPriority
¶
fluid.scheduler.TaskState
¶
Bases: StrEnum
Lifecycle state of a task run.
init
class-attribute
instance-attribute
¶
Task has been created but not yet queued.
queued
class-attribute
instance-attribute
¶
Task is waiting in the queue to be picked up by a worker.
running
class-attribute
instance-attribute
¶
Task is currently being executed.
failure
class-attribute
instance-attribute
¶
Task raised an exception during execution.
aborted
class-attribute
instance-attribute
¶
Task was cancelled before completion.
rate_limited
class-attribute
instance-attribute
¶
Task execution was deferred due to rate limiting.
interrupted
class-attribute
instance-attribute
¶
Task was interrupted by a worker shutdown before it could complete.
fluid.scheduler.K8sConfig
pydantic-model
¶
Bases: BaseModel
Kubernetes configuration for tasks run on Kubernetes cluster. This configuration is used by the task consumer to run tasks on Kubernetes Jobs.
This is used when the task consumer runs inside a Kubernetes cluster and the task is marked as CPU bound.
Fields:
-
namespace(str) -
deployment(str) -
container(str) -
resources(K8sResourceRequirements | None) -
job_ttl(int) -
sleep(float)
resources
pydantic-field
¶
Kubernetes resource limits and requests for the container
fluid.scheduler.K8sResourceRequirements
¶
fluid.scheduler.is_in_cpu_process
¶
Check if the current process is a CPU process.
A CPU process is a process that is spawned by the task manager to run a cpu-bound task.
It is identified by the environment variable TASK_MANAGER_SPAWN
being set to "true".