Skip to content

Task class

Bases: _Entity, _Labeled

Hold a user function that will be executed, its parameters and the results.

A Task brings together the user code as function, the inputs and the outputs as data nodes (instances of the DataNode class).

Note

It is not recommended to instantiate a Task directly. Instead, it should be created with the create_scenario() function. When creating a Scenario, the related data nodes and tasks are created automatically. Please refer to the Scenario class for more information.

A task's attributes (the input data nodes, the output data nodes, the Python function) are populated based on its task configuration TaskConfig.

Example
import taipy as tp
from taipy import Config

def by_two(x: int):
    return x * 2

if __name__ == "__main__":
    # Configure data nodes, tasks and scenarios
    input_cfg = Config.configure_data_node("my_input", default_data=2)
    result_cfg = Config.configure_data_node("my_result")
    task_cfg = Config.configure_task("my_double", function=by_two, input=input_cfg, output=result_cfg)
    scenario_cfg = Config.configure_scenario("my_scenario", task_configs=[task_cfg])

    # Instantiate a task along with a scenario
    sc = tp.create_scenario(scenario_cfg)

    # Retrieve task and data nodes from scenario
    task_input = sc.my_input
    double_task = sc.my_double
    task_result = sc.my_result

    # Write the input data and submit the task
    task_input.write(3)
    double_task.submit()

    # Read the result
    print(task_result.read())  # Output: 6

    # Retrieve the list of all tasks
    all_tasks = tp.get_tasks()

Attributes:

Name Type Description
config_id str

The identifier of the TaskConfig.

properties dict[str, Any]

A dictionary of additional properties.

function callable

The python function to execute. The function must take as parameter the data referenced by inputs data nodes, and must return the data referenced by outputs data nodes.

input Union[DataNode, List[DataNode]]

The list of inputs.

output Union[DataNode, List[DataNode]]

The list of outputs.

id str

The unique identifier of the task.

owner_id str

The identifier of the owner (sequence_id, scenario_id, cycle_id) or None.

parent_ids Optional[Set[str]]

The set of identifiers of the parent sequences.

version str

The string indicates the application version of the task to instantiate. If not provided, the latest version is used.

skippable bool

If True, indicates that the task can be skipped if no change has been made on inputs. The default value is False.

Attributes

config_id property

config_id: str

The identifier of the TaskConfig.

data_nodes property

data_nodes: Dict[str, DataNode]

The dictionary of input and output data nodes.

function property writable

function: Callable

The python function to execute.

id instance-attribute

id: TaskId = id or TaskId(
    join([_ID_PREFIX, config_id, str(uuid4())])
)

The unique identifier of the task.

input property

input: Dict[str, DataNode]

The dictionary of input data nodes.

output property

output: Dict[str, DataNode]

The dictionary of output data nodes.

owner_id property

owner_id: Optional[str]

The identifier of the owner (scenario_id or cycle_id) or None.

parent_ids property

parent_ids: Set[str]

The set of identifiers of the parent scenarios.

properties property

properties: _Properties

Dictionary of additional properties.

scope property

scope: Scope

The lowest scope of the task's data nodes.

The lowest scope present in input and output data nodes or GLOBAL if there are either no input or no output.

skippable property writable

skippable: bool

True if the task can be skipped if no change has been made on inputs. False otherwise

version property

version: str

The application version of the task.

The string indicates the application version of the task. If not provided, the latest version is used.

Methods

get_label()

get_label() -> str

Returns the task simple label prefixed by its owner label.

Returns:

Type Description
str

The label of the task as a string.

get_parents()

get_parents() -> Dict[str, Set[_Entity]]

Get the parent scenarios of the task.

Returns:

Type Description
Dict[str, Set[_Entity]]

The dictionary of all parent entities. They are grouped by their type (Scenario^, Sequences^, or tasks^) so each key corresponds to a level of the parents and the value is a set of the parent entities. An empty dictionary is returned if the entity does not have parents.

get_simple_label()

get_simple_label() -> str

Returns the task simple label.

Returns:

Type Description
str

The simple label of the task as a string.

submit()

submit(
    callbacks: Optional[List[Callable]] = None,
    force: bool = False,
    wait: bool = False,
    timeout: Optional[Union[float, int]] = None,
    **properties
) -> Submission

Submit the task for execution.

Parameters:

Name Type Description Default
callbacks List[Callable]

The list of callable functions to be called on status change.

None
force bool

Force execution even if the data nodes are in cache.

False
wait bool

Wait for the orchestrated job created from the task submission to be finished in asynchronous mode.

False
timeout Union[float, int]

The maximum number of seconds to wait for the job to be finished before returning.
If not provided and wait is True, the function waits indefinitely.

None
**properties dict[str, any]

A keyworded variable length list of additional arguments.

{}

Returns:

Type Description
Submission

A Submission containing the information of the submission.