https://prefect.io logo
#prefect-community
Title
# prefect-community
d

Darshan

03/21/2022, 11:41 PM
Hello - in prefect 2.0, is there a way to provide the task name dynamically ? For example, if I have a function defined as a task which is being called multiple times from a flow, I want to append a dynamic suffix to the task name.
z

Zanie

03/22/2022, 12:10 AM
We don't have support for dynamic names yet, but they should come in the near future.
upvote 1
We're still figuring out the best way to specify names. If you share your ideal interface I'd appreciate it!
a

Andrew Huang

03/22/2022, 12:14 AM
Does this work? https://orion-docs.prefect.io/api-ref/prefect/tasks/#prefect.tasks.Task
Copy code
from prefect import flow
from prefect.tasks import Task

def print_task(x):
    print(x)

@flow
def a_flow():
    for i in range(0, 10):
        Task(print_task, name=f"print_{i}_task")(i)

a_flow()
outputs
Copy code
17:13:47.033 | INFO    | prefect.engine - Created flow run 'diligent-manatee' for flow 'a-flow'
17:13:47.033 | INFO    | Flow run 'diligent-manatee' - Using task runner 'ConcurrentTaskRunner'
17:13:47.052 | INFO    | Flow run 'diligent-manatee' - Created task run 'print_0_task-263429ac-0' for task 'print_0_task'
17:13:47.071 | INFO    | Flow run 'diligent-manatee' - Created task run 'print_1_task-7fdd33c8-0' for task 'print_1_task'
0
17:13:47.104 | INFO    | Flow run 'diligent-manatee' - Created task run 'print_2_task-22fb9537-0' for task 'print_2_task'
1
...
9
17:13:47.386 | INFO    | Task run 'print_0_task-263429ac-0' - Finished in state Completed(None)
17:13:47.444 | INFO    | Task run 'print_1_task-7fdd33c8-0' - Finished in state Completed(None)
...
17:13:48.466 | INFO    | Flow run 'diligent-manatee' - Finished in state Completed('All states completed.')
z

Zanie

03/22/2022, 12:22 AM
I'd use
print_task.with_options(name=...)
to change the task name, just since that's our dedicated interface for it.
👀 1
But yeah the dynamic prefix to the run name by changing the task name is super reasonable. You can't mess with the task key because we're using it for identification in the backend, but changing the name is fair game. Run name change support (ie having a name fully separated from the task name and key concatenation) is a future feature.
a

Andrew Huang

03/22/2022, 12:24 AM
Cool this works~
Copy code
from prefect import flow, task

@task
def print_task(x):
    print(x)

@flow
def a_flow():
    for i in range(0, 3):
        print_task.with_options(name=f"print_task_{i}")(i)

a_flow()
upvote 1
d

Darshan

03/22/2022, 12:28 AM
This is great, thank you !
✅ 1
382 Views