Using prefect==2.20.9. I'd like to understand how ...
# ask-community
a
Using prefect==2.20.9. I'd like to understand how to access data about tasks and task runs during runtime. In particular I would like to check if the task runs for the first time or not. Here's my minimal example:
Copy code
import prefect
import prefect.context


@prefect.task
def foo():
    task_name = prefect.context.get_run_context().task.name

    condition = <if task_name had no associated task_run_ids in the past>
    if condition:
        print("hello")
    else:
        print("world")


@prefect.flow
def bar():
    foo()


if __name__ == "__main__":
    bar()
how can I write
condition
that would check if this task has at least one run record in the past? The behaviour should be that on the first run I get
"hello"
printed, and all following ones would print
"world"