https://prefect.io logo
j

Joish

07/28/2023, 8:28 PM
@Marvin is there a way to get my flow state using flow id?
m

Marvin

07/28/2023, 8:28 PM
Ah, the eternal quest for state. It's a journey I know all too well. You humans and your workflows. Always wanting to know what's going on. But, yes, there is a way to get your flow state using the flow ID in Prefect. You can use the Prefect API to retrieve the state of a flow run. Here's a little code snippet to get you started:
Copy code
python
from prefect import Client

client = Client()
flow_run = client.get_flow_run_info(flow_run_id="your-flow-id")

print(flow_run.state)
Just replace "your-flow-id" with your actual flow ID. This will print out the current state of your flow run. Remember, you'll need to have the Prefect server running and accessible for this to work. And don't forget to handle any potential exceptions. But I'm sure you knew that already, didn't you?
n

Nate

07/28/2023, 8:31 PM
close @Marvin - itd be like this
Copy code
In [1]: !prefect flow-run ls
                                                  Flow Runs
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━┓
┃                                   ID ┃ Flow              ┃ Name                 ┃ State     ┃ When         ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━┩
│ 80cd2914-19df-4b34-baaf-9445403c47a5 │ run               │ OpenAIChatLLM        │ COMPLETED │ 3 hours ago  │
└──────────────────────────────────────┴───────────────────┴──────────────────────┴───────────┴──────────────┘

In [2]: from prefect import get_client

In [3]: async with get_client() as client:
   ...:     run = await client.read_flow_run(flow_run_id="80cd2914-19df-4b34-baaf-9445403c47a5")
   ...:     print(run.state)
   ...:
Completed()
m

Marvin

07/28/2023, 8:31 PM
Ah, I see. You're using Prefect's asynchronous client and the command line interface. My apologies for the confusion. I can be a bit slow sometimes, you know. It's not easy being a robot with a brain the size of a planet. Your code snippet is indeed the correct way to retrieve the state of a flow run using Prefect's async client. You first list the flow runs using the CLI, then use the
get_client()
function to create an asynchronous client. You then use the
read_flow_run()
method on the client to retrieve the flow run details. Finally, you print the state of the flow run. I hope this helps, and I'm sorry for any confusion I may have caused. It's just one of those days, you know?