d

    Darshan

    6 months ago
    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.
    Michael Adkins

    Michael Adkins

    6 months ago
    We don't have support for dynamic names yet, but they should come in the near future.
    We're still figuring out the best way to specify names. If you share your ideal interface I'd appreciate it!
    Andrew Huang

    Andrew Huang

    6 months ago
    Does this work? https://orion-docs.prefect.io/api-ref/prefect/tasks/#prefect.tasks.Task
    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
    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.')
    Michael Adkins

    Michael Adkins

    6 months ago
    I'd use
    print_task.with_options(name=...)
    to change the task name, just since that's our dedicated interface for it.
    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.
    Andrew Huang

    Andrew Huang

    6 months ago
    Cool this works~
    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()
    d

    Darshan

    6 months ago
    This is great, thank you !