https://prefect.io logo
e

Emil Christensen

07/06/2023, 3:34 PM
Example of getting the current retry count within a flow
Copy code
from prefect import flow, get_run_logger
from prefect.context import get_run_context

@flow(retries=3)
def main():
    get_run_logger().warning(f"This is run {get_run_context().flow_run.run_count}")
    raise ValueError()

if __name__ == "__main__":
    main()
✅ 1
@dainslie were you asking about this?
d

dainslie

07/06/2023, 3:35 PM
Nice one. Thanks.
t

Theo Platt

07/06/2023, 3:38 PM
@Emil Christensen I just tried that and got -
Copy code
File "/Users/theoplatt/git/prefectcourse/pacc102.py", line 9, in try_again
    log.warning(f"This is run {get_run_context().flow_run.run_count}")
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'TaskRunContext' object has no attribute 'flow_run'
d

dainslie

07/06/2023, 3:41 PM
Just tried and same for me.
t

Theo Platt

07/06/2023, 3:42 PM
solved it
Copy code
get_run_context().task_run.run_count
task_run
not
flow_run
when in a task
đŸĒŠ 1
d

dainslie

07/06/2023, 3:44 PM
đŸ•ē
Though count looks funny when I print it out:
Copy code
Count = $1
t

Theo Platt

07/06/2023, 3:45 PM
I'm doing this -
Copy code
log = get_run_logger()
    log.warning(f"This is run {get_run_context().task_run.run_count}")
and I get -
Copy code
16:42:36.024 | WARNING | Task run 'try_again-0' - This is run 2
16:42:36.549 | INFO    | Task run 'try_again-0' - Failed
16:42:36.550 | ERROR   | Task run 'try_again-0' - Encountered exception during execution:
d

dainslie

07/06/2023, 3:46 PM
đŸ•ē
Working nicely now. Thanks @Emil Christensen @Theo Platt
🙌 1