Bino Oetomo
Bino Oetomo

Reputation: 625

How to get own Flow Run ID?

I modified the simple example 'favorite_number.py':

from prefect import flow

@flow
def my_favorite_function(myvar:str='1'):
    print(f'\n\nLOCALS: {locals()}\n\n')

At the agent side, the output is :

Agent started! Looking for work from queue 'Agent queue semua'...
12:51:44.174 | INFO    | prefect.agent - Submitting flow run '4f147b53-2db8-4762-9f51-f0031a8f7eb1'
12:51:44.257 | INFO    | prefect.infrastructure.process - Opening process 'stalwart-sparrow'...
12:51:44.261 | INFO    | prefect.agent - Completed submission of flow run '4f147b53-2db8-4762-9f51-f0031a8f7eb1'
12:51:47.574 | INFO    | Flow run 'stalwart-sparrow' - Finished in state Completed()


LOCALS: {'myvar': '1'}


12:51:48.012 | INFO    | prefect.infrastructure.process - Process 'stalwart-sparrow' exited cleanly.

I found that '4f147b53-2db8-4762-9f51-f0031a8f7eb1' is the flow_run id.

My question is : How to make that my_favorite_function print it's flow_run_id? Actually I need that flow_run_id for another process outside prefect.

Sincerely
-bino-

Upvotes: 1

Views: 643

Answers (1)

Anna Geller
Anna Geller

Reputation: 1758

Here is how you can get your favorite function to print the flow run ID in Prefect 2:

import prefect
from prefect import flow


@flow
def my_favorite_function():
    run_id = prefect.context.get_run_context().flow_run.id
    print(run_id)


if __name__ == "__main__":
    my_favorite_function()

Upvotes: 3

Related Questions