<@ULVA73B9P> is there a way I can rename the task ...
# ask-marvin
b
@Marvin is there a way I can rename the task name on the go ? I have the same task being triggered multiple times
m
Yes, you can dynamically rename a task in Prefect 3.x by providing a custom name for each task run. You can achieve this by using the
task_run_name
parameter with Python's standard string formatting syntax. This allows you to distinguish runs of the same task by providing a unique name for each execution. Here's a basic example:
Copy code
python
from prefect import task, flow
from prefect.runtime import context

@task(task_run_name=lambda: f"dynamic_name_{context.task_run_id}")
def my_task():
    print("Executing task")

@flow
def my_flow():
    for _ in range(3):
        my_task()

my_flow()
In this example, the task name will include the task run ID, making each execution unique. For more detailed information, you can explore the Prefect documentation on writing tasks. If you have further requirements or need additional help, feel free to ask.
b
Can I pass something as name that can be appended to task name ? @Marvin
m
Yes, you can append additional information to the task name by passing a parameter to the task and using it within the
task_run_name
function. Here's how you can do it:
Copy code
python
from prefect import task, flow

@task(task_run_name=lambda x: f"task_with_{x}")
def my_task(x):
    print(f"Executing task with {x}")

@flow
def my_flow():
    for i in range(3):
        my_task(i)

my_flow()
In this example, the task name will include the value of
x
, which is passed as a parameter to the task. This way, each task run will have a unique name based on the parameter value.