# Task
# Task
class
prefect.core.task.Task
(name=None, slug=None, tags=None, max_retries=None, retry_delay=None, timeout=None, trigger=None, skip_on_upstream_skip=True, cache_for=None, cache_validator=None, cache_key=None, checkpoint=None, state_handlers=None, on_failure=None, log_stdout=False, result=None, target=None, task_run_name=None, nout=None)[source]The Task class which is used as the full representation of a unit of work.
This Task class can be used directly as a first class object where it must be inherited from by a class that implements the run
method. For a more functional way of generating Tasks, see the task decorator.
Inheritance example:
class AddTask(Task):
def run(self, x, y):
return x + y
Note: The implemented run
method cannot have *args
in its signature. In addition, the following keywords are reserved: upstream_tasks
, task_args
and mapped
.
An instance of a Task
can be used functionally to generate other task instances with the same attributes but with different values bound to their run
methods.
Example:
class AddTask(Task):
def run(self, x, y):
return x + y
a = AddTask()
with Flow("My Flow") as f:
t1 = a(1, 2) # t1 != a
t2 = a(5, 7) # t2 != a
To bind values to a Task's run method imperatively (and without making a copy), see Task.bind
.
Args:
name (str, optional)
: The name of this taskslug (str, optional)
: The slug for this task. Slugs provide a stable ID for tasks so that the Prefect API can identify task run states. If a slug is not provided, one will be generated automatically once the task is added to a Flow.tags ([str], optional)
: A list of tags for this taskmax_retries (int, optional)
: The maximum amount of times this task can be retriedretry_delay (timedelta, optional)
: The amount of time to wait until task is retriedtimeout (Union[int, timedelta], optional)
: The amount of time (in seconds) to wait while running this task before a timeout occurs; note that sub-second resolution is not supported, even when passing in a timedelta.trigger (callable, optional)
: a function that determines whether the task should run, based on the states of any upstream tasks.skip_on_upstream_skip (bool, optional)
: ifTrue
, if any immediately upstream tasks are skipped, this task will automatically be skipped as well, regardless of trigger. By default, this prevents tasks from attempting to use either state or data from tasks that didn't run. IfFalse
, the task's trigger will be called as normal, with skips considered successes. Defaults toTrue
.cache_for (timedelta, optional)
: The amount of time to maintain a cache of the outputs of this task. Useful for situations where the containing Flow will be rerun multiple times, but this task doesn't need to be.cache_validator (Callable, optional)
: Validator that will determine whether the cache for this task is still valid (only required ifcache_for
is provided; defaults toprefect.engine.cache_validators.duration_only
)cache_key (str, optional)
: if provided, acache_key
serves as a unique identifier for this Task's cache, and can be shared across both Tasks and Flows; if not provided, the Task's name will be used if running locally, or the Task's database ID if running in Cloudcheckpoint (bool, optional)
: if this Task is successful, whether to store its result using the configured result available during the run; Also note that checkpointing will only occur locally ifprefect.config.flows.checkpointing
is set toTrue
result (Result, optional)
: the result instance used to retrieve and store task results during executiontarget (Union[str, Callable], optional)
: location to check for task Result. If a result exists at that location then the task run will enter a cached state.target
strings can be templated formatting strings which will be formatted at runtime with values fromprefect.context
. If a callable function is provided, it should have signaturecallable(**kwargs) -> str
and at write time all formatting kwargs will be passed and a fully formatted location is expected as the return value. The callable can be used for string formatting logic that.format(**kwargs)
doesn't support.state_handlers (Iterable[Callable], optional)
: A list of state change handlers that will be called whenever the task changes state, providing an opportunity to inspect or modify the new state. The handler will be passed the task instance, the old (prior) state, and the new (current) state, with the following signature:state_handler(task: Task, old_state: State, new_state: State) -> Optional[State]
If multiple functions are passed, then thenew_state
argument will be the result of the previous handler.on_failure (Callable, optional)
: A function with signaturefn(task: Task, state: State) -> None
that will be called anytime this Task enters a failure statelog_stdout (bool, optional)
: Toggle whether or not to send stdout messages to the Prefect logger. Defaults toFalse
.task_run_name (Union[str, Callable], optional)
: a name to set for this task at runtime.task_run_name
strings can be templated formatting strings which will be formatted at runtime with values from task arguments,prefect.context
, and flow parameters (in the case of a name conflict between these, earlier values take precedence). If a callable function is provided, it should have signaturecallable(**kwargs) -> str
and at write time all formatting kwargs will be passed and a fully formatted location is expected as the return value. The callable can be used for string formatting logic that.format(**kwargs)
doesn't support. Note: this only works for tasks running against a backend API.nout (int, optional)
: for tasks that return multiple results, the number of outputs to expect. If not provided, will be inferred from the task return annotation, if possible. Note thatnout=1
implies the task returns a tuple of one value (leave asNone
for non-tuple return types).
TypeError
: iftags
is of typestr
TypeError
: iftimeout
is not of typeint
methods: |
---|
prefect.core.task.Task.bind (*args, mapped=False, upstream_tasks=None, flow=None, **kwargs)[source] |
Binding a task to (keyword) arguments creates a keyed edge in the active Flow that will pass data from the arguments (whether Tasks or constants) to the Task's
|
prefect.core.task.Task.copy (**task_args)[source] |
Creates and returns a copy of the current Task.
|
prefect.core.task.Task.inputs ()[source] |
Describe the inputs for this task. The result is a dictionary that maps each input to a
|
prefect.core.task.Task.is_equal (other)[source] |
Produces a Task that evaluates
|
prefect.core.task.Task.is_not_equal (other)[source] |
Produces a Task that evaluates
|
prefect.core.task.Task.map (*args, upstream_tasks=None, flow=None, task_args=None, **kwargs)[source] |
Map the Task elementwise across one or more Tasks. Arguments that should not be mapped over should be placed in the
will map over the values of X , but not over the values of Y Args:
|
prefect.core.task.Task.not_ ()[source] |
Produces a Task that evaluates
|
prefect.core.task.Task.or_ (other)[source] |
Produces a Task that evaluates
|
prefect.core.task.Task.outputs ()[source] |
Get the output types for this task.
|
prefect.core.task.Task.run ()[source] |
The
|
prefect.core.task.Task.serialize ()[source] |
Creates a serialized representation of this task
|
prefect.core.task.Task.set_dependencies (flow=None, upstream_tasks=None, downstream_tasks=None, keyword_tasks=None, mapped=False, validate=None)[source] |
Set dependencies for a flow either specified or in the current context using this task
|
prefect.core.task.Task.set_downstream (task, flow=None, key=None, mapped=False)[source] |
Sets the provided task as a downstream dependency of this task.
|
prefect.core.task.Task.set_upstream (task, flow=None, key=None, mapped=False)[source] |
Sets the provided task as an upstream dependency of this task.
|
This documentation was auto-generated from commit n/a
on July 1, 2021 at 18:35 UTC