Skip to content

Task

This page describes how to manage tasks in Taipy. It explains how to configure, create and use tasks.

A Task is a submittable entity that represents a function to execute. It is created from a TaskConfig and can be submitted to the Taipy orchestration for execution. It brings together the user code as function, the inputs and the outputs as data nodes (instances of the DataNode class).

Task configuration

A task configuration is necessary to instantiate a Task. To create a TaskConfig, you can use the Config.configure_task() method with the following parameters:

  • id: The id of the task configuration to be created. This id is mandatory and must be a unique and valid Python identifier.
  • function: The function to execute.
  • inputs: The input data nodes referring to the function's parameter(s) data to be executed.
  • outputs: The output data nodes referring to the result(s) data of the function to be executed.
  • skippable: Boolean attribute indicating if the task execution can be skipped if all output data nodes are up-to-date (see the validity_period attribute in the data node configuration page). The default value of skippable is False.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from taipy import Config


def double(nb):
    return nb * 2


input_data_node_cfg = Config.configure_data_node("my_input",
                                                 default_data=21)
output_data_node_cfg = Config.configure_data_node("my_output")

double_task_cfg = Config.configure_task("double_task",
                                        double,
                                        input_data_node_cfg,
                                        output_data_node_cfg,
                                        skippable=True)

In the example above, we created a TaskConfig named double_task_cfg.

In lines 4-5, we define a function double(), to be used in a Task instantiated from the task config. It takes a single parameter and returns a single value.

In lines 8-9, two data node configurations are created. They will be used respectively as argument and result of the function double().

Finally, on line 12-16, we create the task configuration with the id double_task. It represents the function double() that expects an input data node as a parameter and returns an output data node. On line 13, the Task configuration has been set as skippable. That means when submitting a Task entity instantiated from this TaskConfig, Taipy will skip its execution if its input data nodes haven't changed since the previous execution.

Because a Task can have several inputs and outputs, Config.configure_task() can receive lists of DataNodeConfig objects.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from taipy import Config


def multiply_and_add(nb1, nb2):
    return nb1 * nb2, nb1 + nb2


nb_1_cfg = Config.configure_data_node("nb_1", default_data=21)
nb_2_cfg = Config.configure_data_node("nb_2", default_data=2)

multiplication_cfg = Config.configure_data_node("multiplication")
addition_cfg = Config.configure_data_node("addition")

task_cfg = Config.configure_task("foo",
                                 multiply_and_add,
                                 [nb_1_cfg, nb_2_cfg],
                                 [multiplication_cfg, addition_cfg])

In lines 4-5, we define a function with two parameters and two return values.

In lines 8-9, two data nodes configurations are created. They will be used as the function arguments.

In lines 11-12, two data nodes are configured. They will be used as the function results.

Finally, in lines 14-17, we create the task configuration with the id foo representing the function multiply_and_add. It expects two input data nodes and two output data nodes.

Task creation

Tasks get created when scenarios are created using the taipy.create_scenario() method.

For more details, see the scenario creation page.

Task attributes

A Task entity is identified by a unique identifier id Taipy generates. A task also holds various properties accessible as an attribute of the task:

  • config_id is the id of the scenario configuration.
  • input is the list of input data nodes.
  • output is the list of output data nodes.
  • function is the Python function associated with the Task config.
    The function takes as many parameters as there are data nodes in the input attribute. Each parameter corresponds to the return value of an input data node read() method.
    The function returns as many parameters as there are data nodes in the output attribute. Each function's returned value corresponds to the parameter of an output data node write() method.
  • version: The string indicates the application version of the task to instantiate. If not provided, the current version is used. Refer to the version management page for more details.
  • skippable: Boolean attribute indicating if a task execution can be skipped when all output data nodes are up-to-date (see the validity_period attribute in the data node management page). The default value of skippable is False.

Example

The code below uses the monthly_scenario_cfg configuration imported from the my_config.py module to shows the task attributes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import taipy as tp
import my_config

if __name__ == "__main__":
    scenario = tp.create_scenario(my_config.monthly_scenario_cfg)
    task = scenario.predicting

    # the config_id is an attribute of the task. Here it equals "predicting"
    task.config_id

    # the function to be executed with data from input data
    # nodes and returns value for output data nodes.
    task.function  # predict

    # input is the list of input data nodes of the task
    task.input  # [trained_model_cfg, current_month_cfg]

    # output is the list of output data nodes of the task
    task.output  # [sales_predictions_cfg]

    # the current_month data node entity is exposed as an attribute of the task
    current_month_data_node = task.current_month

Get tasks

Get tasks by id

The first method to access a task is from its id by using the taipy.get() method.

Example

The code below uses the monthly_scenario_cfg configuration imported from the my_config.py module to demonstrate how to get a task by its id.

1
2
3
4
5
6
7
8
import taipy as tp
import my_config

if __name__ == "__main__":
    scenario = tp.create_scenario(my_config.monthly_scenario_cfg)
    task = scenario.training
    task_retrieved = tp.get(task.id)
    # task == task_retrieved

Here, the two variables task and task_retrieved are equal.

Get tasks by config id

A task can be retrieved from a scenario or a sequence, by accessing the task config_id attribute.

Example

The code below uses the monthly_scenario_cfg configuration imported from the my_config.py module to demonstrate how to get a task by its configuration id.

1
2
3
4
5
6
7
8
9
import taipy as tp
import my_config

if __name__ == "__main__":
    scenario = tp.create_scenario(my_config.monthly_scenario_cfg)
    task_1 = scenario.predicting  # "predicting" is the config_id of the task
    sequence = scenario.sales
    task_2 = sequence.predicting  # "predicting" is the config_id of the task
    # task_1 == task_2

Tasks can also be retrieved using taipy.get_entities_by_config_id() providing the config_id. This method returns the list of all existing tasks instantiated from the config_id provided as a parameter.

Example

The code below uses the monthly_scenario_cfg configuration imported from the my_config.py module to demonstrate how to get tasks by configuration id.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import taipy as tp
import my_config

if __name__ == "__main__":
    # Create 2 scenarios, which will also create 2 trainig tasks.
    scenario_1 = tp.create_scenario(my_config.monthly_scenario_cfg)
    scenario_2 = tp.create_scenario(my_config.monthly_scenario_cfg)

    # Get all training tasks by config id, this will return a list of 2 training tasks
    # created alongside the 2 scenarios.
    all_training_tasks = tp.get_entities_by_config_id("training")

Get all tasks

All tasks that are part of a scenario or a sequence can be directly accessed as attributes:

Example

The code below uses the monthly_scenario_cfg configuration imported from the my_config.py module to demonstrate how to get all tasks from a scenario.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import taipy as tp
import my_config

if __name__ == "__main__":
    # Creating a scenario from a config
    scenario = tp.create_scenario(my_config.monthly_scenario_cfg)

    # Access all the tasks from the scenario
    scenario.tasks

    # Access the sequence 'sales' from the scenario and
    # then access all the tasks from the sequence
    sequence = scenario.sales
    sequence.tasks

All the tasks can be retrieved using the method taipy.get_tasks(). which returns the list of all existing tasks.

Example

1
2
3
4
import taipy as tp

# Retrieve all tasks
tasks = tp.get_tasks()

Get parent scenarios and sequences

To access the parent entities of a task (scenarios or sequences), you can use either the method Task.get_parents() or the function taipy.get_parents(). Both return the parents of the task.

Example

The code below uses the monthly_scenario_cfg configuration imported from the my_config.py module to demonstrate how to get parent entities from a task.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import taipy as tp
import my_config

if __name__ == "__main__":
    # Create a scenario from a config
    scenario = tp.create_scenario(my_config.monthly_scenario_cfg)

    # Retrieve a task
    task = scenario.training_cfg

    # Retrieve the parent entities of the task. The returned value is
    # {'scenarios': [Scenario 1], 'sequences': [Sequence 1]}
    parent_entities = task.get_parents()

    # Retrieve the parent entities of the task. The return value is
    # {'scenarios': [Scenario 1], 'sequences': [Sequence 1]}
    tp.get_parents(task)

Submit a task

A Task is a submittable entity. You can submit a task with the taipy.submit() function or the Task.submit() method. Submitting a task automatically creates a Job modeling the Task execution and returns a Submission object containing the information about the submission of the task such as the created Job and the submission status.

For more details and examples on how to submit a task, see the scenario submission page.