Shahid Khan
02/05/2025, 8:17 AMMarvin
02/05/2025, 8:17 AMtags context manager from the prefect module. Here's a concise example:
python
from prefect import flow, tags
from prefect.events.schemas import DeploymentTrigger
TAG_NAME = "foobar"
@flow
def upstream():
pass
@flow
def downstream():
pass
if __name__ == "__main__":
with tags(TAG_NAME):
upstream()
upstream()
upstream()
downstream.serve(
name="downstream-of-tagged-upstream",
triggers=[
DeploymentTrigger(
expect={"prefect.flow-run.Completed"},
match_related=[
{
"prefect.resource.role": "tag",
"prefect.resource.id": f"prefect.tag.{TAG_NAME}",
},
{
"prefect.resource.role": "flow",
"prefect.resource.name": "upstream",
}
],
threshold=3,
)
]
)
In this example, the tags context manager is used to apply a dynamic tag (TAG_NAME) to multiple runs of the upstream flow. The downstream flow is configured to trigger based on the completion of these tagged upstream runs.
For more details, you can view the full example here.Shahid Khan
02/05/2025, 10:34 AMMarvin
02/05/2025, 10:34 AM@task decorator with the tags parameter. Here's a simple example:
python
from prefect import task, flow
@task(tags=["dynamic-tag"])
def my_task():
print("Task is running with dynamic tags!")
@flow
def my_flow():
my_task()
my_flow()
In this example, the my_task function is decorated with @task and assigned a dynamic tag using the tags parameter. You can modify the tags list based on your requirements to apply different tags.
For more details on the @task decorator and its parameters, you can refer to the Prefect documentation.