prefect.tasks
Module containing the base workflow task class and decorator - for most use cases, using the @task decorator is preferred.
Functions
task_input_hash
context: the activeTaskRunContextarguments: a dictionary of arguments to be passed to the underlying task
- a string hash if hashing succeeded, else
None
exponential_backoff
backoff_factor: the base delay for the first retry, subsequent retries will increase the delay time by powers of 2.
- a callable that can be passed to the task constructor
task
name: An optional name for the task; if not provided, the name will be inferred from the given function.description: An optional string description for the task.tags: An optional set of tags to be associated with runs of this task. These tags are combined with any tags defined by aprefect.tagscontext at task runtime.version: An optional string specifying the version of this task definitioncache_key_fn: An optional callable that, given the task run context and call parameters, generates a string key; if the key matches a previous completed state, that state result will be restored instead of running the task again.cache_expiration: An optional amount of time indicating how long cached states for this task should be restorable; if not provided, cached states will never expire.task_run_name: An optional name to distinguish runs of this task; this name can be provided as a string template with the task’s keyword arguments as variables, or a function that returns a string.retries: An optional number of times to retry on task run failureretry_delay_seconds: Optionally configures how long to wait before retrying the task after failure. This is only applicable ifretriesis nonzero. This setting can either be a number of seconds, a list of retry delays, or a callable that, given the total number of retries, generates a list of retry delays. If a number of seconds, that delay will be applied to all retries. If a list, each retry will wait for the corresponding delay before retrying. When passing a callable or a list, the number of configured retry delays cannot exceed 50.retry_jitter_factor: An optional factor that defines the factor to which a retry can be jittered in order to avoid a “thundering herd”.persist_result: A toggle indicating whether the result of this task should be persisted to result storage. Defaults toNone, which indicates that the global default should be used (which isTrueby default).result_storage: An optional block to use to persist the result of this task. Defaults to the value set in the flow the task is called in.result_storage_key: An optional key to store the result in storage at when persisted. Defaults to a unique identifier.result_serializer: An optional serializer to use to serialize the result of this task for persistence. Defaults to the value set in the flow the task is called in.timeout_seconds: An optional number of seconds indicating a maximum runtime for the task. If the task exceeds this runtime, it will be marked as failed.log_prints: If set,printstatements in the task will be redirected to the Prefect logger for the task run. Defaults toNone, which indicates that the value from the flow should be used.refresh_cache: If set, cached results for the cache key are not used. Defaults toNone, which indicates that a cached result from a previous execution with matching cache key is used.on_failure: An optional list of callables to run when the task enters a failed state.on_completion: An optional list of callables to run when the task enters a completed state.retry_condition_fn: An optional callable run when a task run returns a Failed state. Should returnTrueif the task should continue to its retry policy (e.g.retries=3), andFalseif the task should end as failed. Defaults toNone, indicating the task should always continue to its retry policy.viz_return_value: An optional value to return when the task dependency tree is visualized.asset_deps: An optional list of upstream assets that this task depends on.
- A callable
Taskobject which, when called, will submit the task for execution.
Classes
TaskRunNameCallbackWithParameters
Methods:
is_callback_with_parameters
TaskOptions
A TypedDict representing all available task configuration options.
This can be used with Unpack to provide type hints for **kwargs.
Task
A Prefect task definition.
Wraps a function with an entrypoint to the Prefect engine. Calling this class within a flow function
creates a new task run.
To preserve the input and output types, we use the generic type variables P and R for “Parameters” and
“Returns” respectively.
Args:
fn: The function defining the task.name: An optional name for the task; if not provided, the name will be inferred from the given function.description: An optional string description for the task.tags: An optional set of tags to be associated with runs of this task. These tags are combined with any tags defined by aprefect.tagscontext at task runtime.version: An optional string specifying the version of this task definitioncache_policy: A cache policy that determines the level of caching for this taskcache_key_fn: An optional callable that, given the task run context and call parameters, generates a string key; if the key matches a previous completed state, that state result will be restored instead of running the task again.cache_expiration: An optional amount of time indicating how long cached states for this task should be restorable; if not provided, cached states will never expire.task_run_name: An optional name to distinguish runs of this task; this name can be provided as a string template with the task’s keyword arguments as variables, or a function that returns a string.retries: An optional number of times to retry on task run failure.retry_delay_seconds: Optionally configures how long to wait before retrying the task after failure. This is only applicable ifretriesis nonzero. This setting can either be a number of seconds, a list of retry delays, or a callable that, given the total number of retries, generates a list of retry delays. If a number of seconds, that delay will be applied to all retries. If a list, each retry will wait for the corresponding delay before retrying. When passing a callable or a list, the number of configured retry delays cannot exceed 50.retry_jitter_factor: An optional factor that defines the factor to which a retry can be jittered in order to avoid a “thundering herd”.persist_result: A toggle indicating whether the result of this task should be persisted to result storage. Defaults toNone, which indicates that the global default should be used (which isTrueby default).result_storage: An optional block to use to persist the result of this task. Defaults to the value set in the flow the task is called in.result_storage_key: An optional key to store the result in storage at when persisted. Defaults to a unique identifier.result_serializer: An optional serializer to use to serialize the result of this task for persistence. Defaults to the value set in the flow the task is called in.timeout_seconds: An optional number of seconds indicating a maximum runtime for the task. If the task exceeds this runtime, it will be marked as failed.log_prints: If set,printstatements in the task will be redirected to the Prefect logger for the task run. Defaults toNone, which indicates that the value from the flow should be used.refresh_cache: If set, cached results for the cache key are not used. Defaults toNone, which indicates that a cached result from a previous execution with matching cache key is used.on_failure: An optional list of callables to run when the task enters a failed state.on_completion: An optional list of callables to run when the task enters a completed state.on_commit: An optional list of callables to run when the task’s idempotency record is committed.on_rollback: An optional list of callables to run when the task rolls back.retry_condition_fn: An optional callable run when a task run returns a Failed state. Should returnTrueif the task should continue to its retry policy (e.g.retries=3), andFalseif the task should end as failed. Defaults toNone, indicating the task should always continue to its retry policy.viz_return_value: An optional value to return when the task dependency tree is visualized.asset_deps: An optional list of upstream assets that this task depends on.
apply_async
args: Arguments to run the task withkwargs: Keyword arguments to run the task with
- A PrefectDistributedFuture object representing the pending task run
create_local_run
create_run
delay
apply_async with simpler calling semantics.
Avoids having to use explicit “args” and “kwargs” arguments. Arguments
will pass through as-is to the task.
Examples:
Define a task
isclassmethod
ismethod
isstaticmethod
map
map
map
map
map
map
map
*args: Iterable and static arguments to run the tasks withreturn_state: Return a list of Prefect States that wrap the results of each task run.wait_for: Upstream task futures to wait for before starting the task**kwargs: Keyword iterable arguments to run the task with
- A list of futures allowing asynchronous access to the state of the
- tasks
unmapped to treat an iterable argument as a constant
on_commit
on_completion
on_failure
on_rollback
serve
task_runner: The task runner to use for serving the task. If not provided, the default task runner will be used.
submit
submit
submit
submit
submit
submit
*args: Arguments to run the task withreturn_state: Return the result of the flow run wrapped in a Prefect State.wait_for: Upstream task futures to wait for before starting the task**kwargs: Keyword arguments to run the task with
- If
return_stateis False a future allowing asynchronous access to the state of the task - If
return_stateis True a future wrapped in a Prefect State allowing asynchronous access to the state of the task
with_options
name: A new name for the task.description: A new description for the task.tags: A new set of tags for the task. If given, existing tags are ignored, not merged.cache_key_fn: A new cache key function for the task.cache_expiration: A new cache expiration time for the task.task_run_name: An optional name to distinguish runs of this task; this name can be provided as a string template with the task’s keyword arguments as variables, or a function that returns a string.retries: A new number of times to retry on task run failure.retry_delay_seconds: Optionally configures how long to wait before retrying the task after failure. This is only applicable ifretriesis nonzero. This setting can either be a number of seconds, a list of retry delays, or a callable that, given the total number of retries, generates a list of retry delays. If a number of seconds, that delay will be applied to all retries. If a list, each retry will wait for the corresponding delay before retrying. When passing a callable or a list, the number of configured retry delays cannot exceed 50.retry_jitter_factor: An optional factor that defines the factor to which a retry can be jittered in order to avoid a “thundering herd”.persist_result: A new option for enabling or disabling result persistence.result_storage: A new storage type to use for results.result_serializer: A new serializer to use for results.result_storage_key: A new key for the persisted result to be stored at.timeout_seconds: A new maximum time for the task to complete in seconds.log_prints: A new option for enabling or disabling redirection ofprintstatements.refresh_cache: A new option for enabling or disabling cache refresh.on_completion: A new list of callables to run when the task enters a completed state.on_failure: A new list of callables to run when the task enters a failed state.retry_condition_fn: An optional callable run when a task run returns a Failed state. Should returnTrueif the task should continue to its retry policy, andFalseif the task should end as failed. Defaults toNone, indicating the task should always continue to its retry policy.viz_return_value: An optional value to return when the task dependency tree is visualized.
- A new
Taskinstance.
MaterializingTask
A task that materializes Assets.
Args:
assets: List of Assets that this task materializes (can be str or Asset)materialized_by: An optional tool that materialized the asset e.g. “dbt” or “spark”**task_kwargs: All other Task arguments