clearml/examples/automation/programmatic_orchestration.py

50 lines
1.8 KiB
Python
Raw Normal View History

2020-12-22 21:25:37 +00:00
from clearml import Task
2020-05-24 05:21:21 +00:00
from time import sleep
# Initialize the Task Pipe's first Task used to start the Task Pipe
2021-12-31 10:33:23 +00:00
task = Task.init(
"examples", "Simple Controller Task", task_type=Task.TaskTypes.controller
)
2020-05-24 05:21:21 +00:00
# Create a hyper-parameter dictionary for the task
param = dict()
# Connect the hyper-parameter dictionary to the task
param = task.connect(param)
# In this example we pass next task's name as a parameter
2021-12-31 10:33:23 +00:00
param["next_task_name"] = "Toy Base Task"
2020-05-24 05:21:21 +00:00
# This is a parameter name in the next task we want to change
2021-12-31 10:33:23 +00:00
param["param_name"] = "Example_Param"
2020-05-24 05:21:21 +00:00
# This is the parameter value in the next task we want to change
2021-12-31 10:33:23 +00:00
param["param_name_new_value"] = 3
2020-05-24 05:21:21 +00:00
# The queue where we want the template task (clone) to be sent to
2021-12-31 10:33:23 +00:00
param["execution_queue_name"] = "default"
2020-05-24 05:21:21 +00:00
# Simulate the work of a Task
2021-12-31 10:33:23 +00:00
print("Processing....")
2020-05-24 05:21:21 +00:00
sleep(2.0)
2021-12-31 10:33:23 +00:00
print("Done processing :)")
2020-05-24 05:21:21 +00:00
# Get a reference to the task to pipe to.
2021-12-31 10:33:23 +00:00
next_task = Task.get_task(
project_name=task.get_project_name(), task_name=param["next_task_name"]
)
2020-05-24 05:21:21 +00:00
# Clone the task to pipe to. This creates a task with status Draft whose parameters can be modified.
2021-12-31 10:33:23 +00:00
cloned_task = Task.clone(source_task=next_task, name="Auto generated cloned task")
2020-05-24 05:21:21 +00:00
# Get the original parameters of the Task, modify the value of one parameter,
# and set the parameters in the next Task
cloned_task_parameters = cloned_task.get_parameters()
2021-12-31 10:33:23 +00:00
cloned_task_parameters[param["param_name"]] = param["param_name_new_value"]
2020-05-24 05:21:21 +00:00
cloned_task.set_parameters(cloned_task_parameters)
2020-12-22 21:25:37 +00:00
# Enqueue the Task for execution. The enqueued Task must already exist in the clearml platform
2021-12-31 10:33:23 +00:00
print(
"Enqueue next step in pipeline to queue: {}".format(param["execution_queue_name"])
)
Task.enqueue(cloned_task.id, queue_name=param["execution_queue_name"])
2020-05-24 05:21:21 +00:00
# We are done. The next step in the pipe line is in charge of the pipeline now.
2021-12-31 10:33:23 +00:00
print("Done")