prefect.task_worker

Functions

should_try_to_read_parameters

should_try_to_read_parameters(task: Task[P, R], task_run: TaskRun) -> bool
Determines whether a task run should read parameters from the result store.

create_status_server

create_status_server(task_worker: TaskWorker) -> FastAPI

serve

serve(*tasks: Task[P, R])
Serve the provided tasks so that their runs may be submitted to and executed in the engine. Tasks do not need to be within a flow run context to be submitted. You must .submit the same task object that you pass to serve. Args:
  • -: A list of tasks to serve. When a scheduled task run is found for a given task, the task run will be submitted to the engine for execution.
  • -: The maximum number of tasks that can be run concurrently. Defaults to 10. Pass None to remove the limit.
  • -: An optional port on which to start an HTTP server exposing status information about the task worker. If not provided, no status server will run.
  • -: If provided, the task worker will exit after the given number of seconds. Defaults to None, meaning the task worker will run indefinitely.

store_parameters

store_parameters(result_store: ResultStore, identifier: UUID, parameters: dict[str, Any]) -> None
Store parameters for a task run in the result store. Args:
  • result_store: The result store to store the parameters in.
  • identifier: The identifier of the task run.
  • parameters: The parameters to store.

read_parameters

read_parameters(result_store: ResultStore, identifier: UUID) -> dict[str, Any]
Read parameters for a task run from the result store. Args:
  • result_store: The result store to read the parameters from.
  • identifier: The identifier of the task run.
Returns:
  • The parameters for the task run.

Classes

StopTaskWorker

Raised when the task worker is stopped.

TaskWorker

This class is responsible for serving tasks that may be executed in the background by a task runner via the traditional engine machinery. When start() is called, the task worker will open a websocket connection to a server-side queue of scheduled task runs. When a scheduled task run is found, the scheduled task run is submitted to the engine for execution with a minimal EngineContext so that the task run can be governed by orchestration rules. Args:
  • -: A list of tasks to serve. These tasks will be submitted to the engine when a scheduled task run is found.
  • -: The maximum number of tasks that can be run concurrently. Defaults to 10. Pass None to remove the limit.
Methods:

client_id

client_id(self) -> str

started_at

started_at(self) -> Optional[DateTime]

started

started(self) -> bool

limit

limit(self) -> Optional[int]

current_tasks

current_tasks(self) -> Optional[int]

available_tasks

available_tasks(self) -> Optional[int]

handle_sigterm

handle_sigterm(self, signum: int, frame: object) -> None
Shuts down the task worker when a SIGTERM is received.

start

start(self, timeout: Optional[float] = None) -> None
Starts a task worker, which runs the tasks provided in the constructor. Args:
  • timeout: If provided, the task worker will exit after the given number of seconds. Defaults to None, meaning the task worker will run indefinitely.

stop

stop(self)
Stops the task worker’s polling cycle.

execute_task_run

execute_task_run(self, task_run: TaskRun) -> None
Execute a task run in the task worker.