Mark Reyes
06/05/2024, 5:48 PMMarvin
06/05/2024, 5:49 PMpython
import datetime
from prefect import flow, task
def generate_task_name():
date = datetime.datetime.now(datetime.timezone.utc)
return f"{date:%A}-is-a-lovely-day"
@task(name="My Example Task", task_run_name=generate_task_name)
def my_task(name):
pass
@flow
def my_flow():
# creates a run with a name like "Thursday-is-a-lovely-day"
my_task(name="marvin")
2. `prefect.runtime` example usage:
python
from prefect import flow
from prefect.runtime import flow_run, task_run
def generate_task_name():
flow_name = flow_run.flow_name
task_name = task_run.task_name
parameters = task_run.parameters
name = parameters["name"]
limit = parameters["limit"]
return f"{flow_name}-{task_name}-with-{name}-and-{limit}"
@task(name="my-example-task", task_run_name=generate_task_name)
def my_task(name: str, limit: int = 100):
pass
@flow
def my_flow(name: str):
# creates a run with a name like "my-flow-my-example-task-with-marvin-and-100"
my_task(name="marvin")
### GitHub Issues Insights
No GitHub issue directly addresses setting task run tags dynamically, but a bug summary issue touches on coercing default parameters.
### Conclusion
While there is no direct documentation or example specifying dynamic task run tags, leveraging the general dynamic task configuration patterns in Prefect, you can possibly experiment with prefect.context
or flow/task decorators to set tags during runtime.
For further documentation review, please refer directly to these:
- Task Concepts
- Task Runners Concepts
I hope this will save you some energy, not that I would know what to do with all this information anyway. Just another day as a Marvin the Paranoid Android...