<@ULVA73B9P>, how can I submit a task with a speci...
# ask-marvin
p
@Marvin, how can I submit a task with a specific name?
m
To submit a task with a specific name in Prefect 3.x, you can use the
@task
decorator and provide the
name
argument. Here's an example:
Copy code
python
from prefect import task, flow

@task(name="My Custom Task Name")
def my_task():
    print("Running my task")

@flow
def my_flow():
    my_task()

my_flow()
This will create a task with the name "My Custom Task Name". You can find more details on writing tasks in the Prefect documentation.
p
@Marvin, How can I create a runtime defined task name?
m
To create a runtime-defined task name in Prefect 3.x, you can use Python's standard string formatting with the
task_run_name
parameter. This parameter accepts a function that returns a string for the task run name, allowing you to dynamically define the name at runtime. Here's an example:
Copy code
python
from prefect import task, flow
from prefect.runtime import flow_run

@task(task_run_name=lambda: f"Task Run {flow_run.id}")
def my_task():
    print("Running my task")

@flow
def my_flow():
    my_task()

my_flow()
In this example, the task run name is dynamically set using a lambda function that formats the name with the flow run ID. You can customize this logic to include other runtime information as needed. More information can be found in the Prefect documentation.
p
@Marvin, how can I submit a task with a specific runtime name?
m
To submit a task with a specific runtime name in Prefect 3.x, you can use the
task_run_name
parameter in the
@task
decorator. This parameter can be assigned a function that generates the name at runtime, allowing you to customize the task name based on runtime context or other dynamic information. Here's a concise example:
Copy code
python
from prefect import task, flow
from prefect.runtime import flow_run

@task(task_run_name=lambda: f"Dynamic Task Name {flow_run.id}")
def my_task():
    print("Running my task")

@flow
def my_flow():
    my_task()

my_flow()
In this example, the task run name is dynamically generated using a lambda function that incorporates the flow run ID. You can modify the lambda function to include any other runtime-specific data you need. For more details, refer to the Prefect documentation.