docs/v3/api-ref/python/prefect-task_engine.mdx
prefect.task_enginerun_task_sync <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1654" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>run_task_sync(task: 'Task[P, R]', task_run_id: Optional[UUID] = None, task_run: Optional[TaskRun] = None, parameters: Optional[dict[str, Any]] = None, wait_for: Optional['OneOrManyFutureOrResult[Any]'] = None, return_type: Literal['state', 'result'] = 'result', dependencies: Optional[dict[str, set[RunInput]]] = None, context: Optional[dict[str, Any]] = None) -> Union[R, State, None]
run_task_async <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1686" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>run_task_async(task: 'Task[P, R]', task_run_id: Optional[UUID] = None, task_run: Optional[TaskRun] = None, parameters: Optional[dict[str, Any]] = None, wait_for: Optional['OneOrManyFutureOrResult[Any]'] = None, return_type: Literal['state', 'result'] = 'result', dependencies: Optional[dict[str, set[RunInput]]] = None, context: Optional[dict[str, Any]] = None) -> Union[R, State, None]
run_generator_task_sync <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1718" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>run_generator_task_sync(task: 'Task[P, R]', task_run_id: Optional[UUID] = None, task_run: Optional[TaskRun] = None, parameters: Optional[dict[str, Any]] = None, wait_for: Optional['OneOrManyFutureOrResult[Any]'] = None, return_type: Literal['state', 'result'] = 'result', dependencies: Optional[dict[str, set[RunInput]]] = None, context: Optional[dict[str, Any]] = None) -> Generator[R, None, None]
run_generator_task_async <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1779" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>run_generator_task_async(task: 'Task[P, R]', task_run_id: Optional[UUID] = None, task_run: Optional[TaskRun] = None, parameters: Optional[dict[str, Any]] = None, wait_for: Optional['OneOrManyFutureOrResult[Any]'] = None, return_type: Literal['state', 'result'] = 'result', dependencies: Optional[dict[str, set[RunInput]]] = None, context: Optional[dict[str, Any]] = None) -> AsyncGenerator[R, None]
run_task <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1867" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>run_task(task: 'Task[P, Union[R, Coroutine[Any, Any, R]]]', task_run_id: Optional[UUID] = None, task_run: Optional[TaskRun] = None, parameters: Optional[dict[str, Any]] = None, wait_for: Optional['OneOrManyFutureOrResult[Any]'] = None, return_type: Literal['state', 'result'] = 'result', dependencies: Optional[dict[str, set[RunInput]]] = None, context: Optional[dict[str, Any]] = None) -> Union[R, State, None, Coroutine[Any, Any, Union[R, State, None]]]
Runs the provided task.
Args:
task: The task to runtask_run_id: The ID of the task run; if not provided, a new task run
will be createdtask_run: The task run object; if not provided, a new task run
will be createdparameters: The parameters to pass to the taskwait_for: A list of futures to wait for before running the taskreturn_type: The return type to return; either "state" or "result"dependencies: A dictionary of task run inputs to use for dependency trackingcontext: A dictionary containing the context to use for the task run; only
required if the task is running on in a remote environmentReturns:
TaskRunTimeoutError <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L229" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>Raised when a task run exceeds its timeout.
BaseTaskRunEngine <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L234" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>Methods:
compute_transaction_key <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L271" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>compute_transaction_key(self) -> Optional[str]
handle_rollback <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L412" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>handle_rollback(self, txn: Transaction) -> None
is_cancelled <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L262" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>is_cancelled(self) -> bool
is_running <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L368" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>is_running(self) -> bool
Whether or not the engine is currently running a task.
log_finished_message <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L374" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>log_finished_message(self) -> None
record_terminal_state_timing <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L361" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>record_terminal_state_timing(self, state: State) -> None
state <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L257" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>state(self) -> State
SyncTaskRunEngine <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L429" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>Methods:
asset_context <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L795" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>asset_context(self)
begin_run <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L513" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>begin_run(self) -> None
call_hooks <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L476" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>call_hooks(self, state: Optional[State] = None) -> None
call_task_fn <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1024" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>call_task_fn(self, transaction: Transaction) -> Union[ResultRecord[Any], None, Coroutine[Any, Any, R], R]
Convenience method to call the task function. Returns a coroutine if the task is async.
can_retry <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L439" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>can_retry(self, exc_or_state: Exception | State[R]) -> bool
client <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L434" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>client(self) -> SyncPrefectClient
handle_crash <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L742" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>handle_crash(self, exc: BaseException) -> None
handle_exception <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L712" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>handle_exception(self, exc: Exception) -> None
handle_retry <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L657" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>handle_retry(self, exc_or_state: Exception | State[R]) -> bool
Handle any task run retries.
handle_success <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L619" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>handle_success(self, result: R, transaction: Transaction) -> Union[ResultRecord[R], None, Coroutine[Any, Any, R], R]
handle_timeout <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L727" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>handle_timeout(self, exc: TimeoutError) -> None
initialize_run <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L813" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>initialize_run(self, task_run_id: Optional[UUID] = None, dependencies: Optional[dict[str, set[RunInput]]] = None) -> Generator[Self, Any, Any]
Enters a client context and creates a task run if needed.
result <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L604" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>result(self, raise_on_failure: bool = True) -> 'Union[R, State, None]'
run_context <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L988" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>run_context(self)
set_state <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L553" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>set_state(self, state: State[R], force: bool = False) -> State[R]
setup_run_context <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L752" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>setup_run_context(self, client: Optional[SyncPrefectClient] = None)
start <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L917" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>start(self, task_run_id: Optional[UUID] = None, dependencies: Optional[dict[str, set[RunInput]]] = None) -> Generator[None, None, None]
transaction_context <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L961" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>transaction_context(self) -> Generator[Transaction, None, None]
wait_until_ready <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L894" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>wait_until_ready(self) -> None
Sync version: Waits until the scheduled time (if its the future), then enters Running.
AsyncTaskRunEngine <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1041" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>Methods:
asset_context <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1425" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>asset_context(self)
begin_run <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1124" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>begin_run(self) -> None
call_hooks <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1087" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>call_hooks(self, state: Optional[State] = None) -> None
call_task_fn <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1638" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>call_task_fn(self, transaction: AsyncTransaction) -> Union[ResultRecord[Any], None, Coroutine[Any, Any, R], R]
Convenience method to call the task function. Returns a coroutine if the task is async.
can_retry <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1051" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>can_retry(self, exc_or_state: Exception | State[R]) -> bool
client <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1046" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>client(self) -> PrefectClient
handle_crash <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1371" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>handle_crash(self, exc: BaseException) -> None
handle_exception <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1339" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>handle_exception(self, exc: Exception) -> None
handle_retry <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1283" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>handle_retry(self, exc_or_state: Exception | State[R]) -> bool
Handle any task run retries.
handle_success <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1246" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>handle_success(self, result: R, transaction: AsyncTransaction) -> Union[ResultRecord[R], None, Coroutine[Any, Any, R], R]
handle_timeout <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1354" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>handle_timeout(self, exc: TimeoutError) -> None
initialize_run <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1443" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>initialize_run(self, task_run_id: Optional[UUID] = None, dependencies: Optional[dict[str, set[RunInput]]] = None) -> AsyncGenerator[Self, Any]
Enters a client context and creates a task run if needed.
result <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1231" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>result(self, raise_on_failure: bool = True) -> 'Union[R, State, None]'
run_context <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1618" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>run_context(self)
set_state <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1178" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>set_state(self, state: State, force: bool = False) -> State
setup_run_context <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1382" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>setup_run_context(self, client: Optional[PrefectClient] = None)
start <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1546" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>start(self, task_run_id: Optional[UUID] = None, dependencies: Optional[dict[str, set[RunInput]]] = None) -> AsyncGenerator[None, None]
transaction_context <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1592" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>transaction_context(self) -> AsyncGenerator[AsyncTransaction, None]
wait_until_ready <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/task_engine.py#L1523" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>wait_until_ready(self) -> None
Waits until the scheduled time (if its the future), then enters Running.