flytekit.task#
- flytekit.task(_task_function: None = None, task_config: T | None = None, cache: bool | Cache = False, retries: int = 0, interruptible: bool | None = None, deprecated: str = '', timeout: timedelta | int = 0, container_image: str | ImageSpec | None = None, environment: Dict[str, str] | None = None, requests: Resources | None = None, limits: Resources | None = None, secret_requests: List[Secret] | None = None, execution_mode: ExecutionBehavior = ExecutionBehavior.DEFAULT, node_dependency_hints: Iterable[PythonFunctionTask | LaunchPlan | WorkflowBase] | None = None, task_resolver: TaskResolverMixin | None = None, docs: Documentation | None = None, disable_deck: bool | None = None, enable_deck: bool | None = None, deck_fields: Tuple[DeckField, ...] | None = (DeckField.SOURCE_CODE, DeckField.DEPENDENCIES, DeckField.TIMELINE, DeckField.INPUT, DeckField.OUTPUT), pod_template: PodTemplate | None = None, pod_template_name: str | None = None, accelerator: BaseAccelerator | None = None, pickle_untyped: bool = False, shared_memory: Literal[True] | str | None = None, resources: Resources | None = None, **kwargs) Callable[[Callable[[...], FuncOut]], PythonFunctionTask[T]] [source]#
- flytekit.task(_task_function: Callable[[P], FuncOut], task_config: T | None = None, cache: bool | Cache = False, retries: int = 0, interruptible: bool | None = None, deprecated: str = '', timeout: timedelta | int = 0, container_image: str | ImageSpec | None = None, environment: Dict[str, str] | None = None, requests: Resources | None = None, limits: Resources | None = None, secret_requests: List[Secret] | None = None, execution_mode: ExecutionBehavior = ExecutionBehavior.DEFAULT, node_dependency_hints: Iterable[PythonFunctionTask | LaunchPlan | WorkflowBase] | None = None, task_resolver: TaskResolverMixin | None = None, docs: Documentation | None = None, disable_deck: bool | None = None, enable_deck: bool | None = None, deck_fields: Tuple[DeckField, ...] | None = (DeckField.SOURCE_CODE, DeckField.DEPENDENCIES, DeckField.TIMELINE, DeckField.INPUT, DeckField.OUTPUT), pod_template: PodTemplate | None = None, pod_template_name: str | None = None, accelerator: BaseAccelerator | None = None, pickle_untyped: bool = False, shared_memory: Literal[True] | str | None = None, resources: Resources | None = None, **kwargs) Callable[[P], FuncOut] | PythonFunctionTask[T]
This is the core decorator to use for any task type in flytekit.
Tasks are the building blocks of Flyte. They represent users code. Tasks have the following properties
Versioned (usually tied to the git revision SHA1)
Strong interfaces (specified inputs and outputs)
Declarative
Independently executable
Unit testable
For a simple python task,
@task def my_task(x: int, y: typing.Dict[str, str]) -> str: ...
For specific task types
@task(task_config=Spark(), retries=3) def my_task(x: int, y: typing.Dict[str, str]) -> str: ...
Please see some cookbook task examples for additional information.
- Parameters:
_task_function – This argument is implicitly passed and represents the decorated function
task_config – This argument provides configuration for a specific task types. Please refer to the plugins documentation for the right object to use.
cache – Boolean or Cache that indicates how caching is configured.
retries – Number of times to retry this task during a workflow execution.
interruptible – [Optional] Boolean that indicates that this task can be interrupted and/or scheduled on nodes with lower QoS guarantees. This will directly reduce the $/execution cost associated, at the cost of performance penalties due to potential interruptions. Requires additional Flyte platform level configuration. If no value is provided, the task will inherit this attribute from its workflow, as follows: No values set for interruptible at the task or workflow level - task is not interruptible Task has interruptible=True, but workflow has no value set - task is interruptible Workflow has interruptible=True, but task has no value set - task is interruptible Workflow has interruptible=False, but task has interruptible=True - task is interruptible Workflow has interruptible=True, but task has interruptible=False - task is not interruptible
deprecated – A string that can be used to provide a warning message for deprecated task. Absence / empty str indicates that the task is active and not deprecated
timeout – the max amount of time for which one execution of this task should be executed for. The execution will be terminated if the runtime exceeds the given timeout (approximately).
container_image –
By default the configured FLYTE_INTERNAL_IMAGE is used for every task. This directive can be used to provide an alternate image for a specific task. This is useful for the cases in which images bloat because of various dependencies and a dependency is only required for this or a set of tasks, and they vary from the default.
# Use default image name `fqn` and alter the tag to `tag-{{default.tag}}` tag of the default image # with a prefix. In this case, it is assumed that the image like # flytecookbook:tag-gitsha is published alongwith the default of flytecookbook:gitsha @task(container_image='{{.images.default.fqn}}:tag-{{images.default.tag}}') def foo(): ... # Refer to configurations to configure fqns for other images besides default. In this case it will # lookup for an image named xyz @task(container_image='{{.images.xyz.fqn}}:{{images.default.tag}}') def foo2(): ...
environment – Environment variables that should be added for this tasks execution
requests – Specify compute resource requests for your task. For Pod-plugin tasks, these values will apply only to the primary container.
limits – Compute limits. Specify compute resource limits for your task. For Pod-plugin tasks, these values will apply only to the primary container. For more information, please see
flytekit.Resources
.secret_requests –
Keys that can identify the secrets supplied at runtime. Ideally the secret keys should also be semi-descriptive. The key values will be available from runtime, if the backend is configured to provide secrets and if secrets are available in the configured secrets store. Possible options for secret stores are - Vault, Confidant, Kube secrets, AWS KMS etc Refer to
Secret
to understand how to specify the request for a secret. It may change based on the backend provider.Note
During local execution, the secrets will be pulled from the local environment variables with the format {GROUP}_{GROUP_VERSION}_{KEY}, where all the characters are capitalized and the prefix is not used.
execution_mode – This is mainly for internal use. Please ignore. It is filled in automatically.
node_dependency_hints –
A list of tasks, launchplans, or workflows that this task depends on. This is only for dynamic tasks/workflows, where flyte cannot automatically determine the dependencies prior to runtime. Even on dynamic tasks this is optional, but in some scenarios it will make registering the workflow easier, because it allows registration to be done the same as for static tasks/workflows.
For example this is useful to run launchplans dynamically, because launchplans must be registered on flyteadmin before they can be run. Tasks and workflows do not have this requirement.
@workflow def workflow0(): ... launchplan0 = LaunchPlan.get_or_create(workflow0) # Specify node_dependency_hints so that launchplan0 will be registered on flyteadmin, despite this being a # dynamic task. @dynamic(node_dependency_hints=[launchplan0]) def launch_dynamically(): # To run a sub-launchplan it must have previously been registered on flyteadmin. return [launchplan0]*10
task_resolver – Provide a custom task resolver.
disable_deck – (deprecated) If true, this task will not output deck html file
enable_deck – If true, this task will output deck html file
deck_fields – If specified and enble_deck is True, this task will output deck html file with the fields specified in the tuple
docs – Documentation about this task
pod_template – Custom PodTemplate for this task.
pod_template_name – The name of the existing PodTemplate resource which will be used in this task.
accelerator – The accelerator to use for this task.
pickle_untyped – Boolean that indicates if the task allows unspecified data types.
shared_memory – If True, then shared memory will be attached to the container where the size is equal to the allocated memory. If int, then the shared memory is set to that size.
resources – Specify both the request and the limit. When the value is set to a tuple or list, the first value is the request and the second value is the limit. If the value is a single value, then both the requests and limit is set to that value. For example, the Resource(cpu=(“1”, “2”), mem=”1Gi”) will set the cpu request to 1, cpu limit to 2, and mem request to 1Gi.
- Deprecated param cache_serialize:
(deprecated - please use Cache) Boolean that indicates if identical (ie. same inputs) instances of this task should be executed in serial when caching is enabled. This means that given multiple concurrent executions over identical inputs, only a single instance executes and the rest wait to reuse the cached results. This parameter does nothing without also setting the cache parameter.
- Deprecated param cache_version:
(deprecated - please use Cache) Cache version to use. Changes to the task signature will automatically trigger a cache miss, but you can always manually update this field as well to force a cache miss. You should also manually bump this version if the function body/business logic has changed, but the signature hasn’t.
- Deprecated param cache_ignore_input_vars:
(deprecated - please use Cache) Input variables that should not be included when calculating hash for cache.