<@ULVA73B9P> after flow_run = await client.read_fl...
# marvin-in-the-wild
p
@Marvin after flow_run = await client.read_flow_run(sample_flow_run), how do I get the flow_run result, the flow result was persisted.
m
To retrieve the result of a flow run, you can use the persisted result storage. Here's an example of how to achieve that: 1. Ensure that your tasks within the flow have
persist_result=True
and appropriate
result_storage_key
. 1. For retrieving the flow run result, use Prefect's API to access the stored result. Here’s a practical example:
Copy code
python
from prefect import flow, task
from prefect.filesystems import S3

@task(persist_result=True, result_storage_key="my_task.json")
def my_task():
    return "Task result"

@flow(result_storage=S3(bucket_path="my-bucket"))
def my_flow():
    return my_task()

my_flow()  # The task's result will be persisted to '<s3://my-bucket/my_task.json>'
After executing the flow, you can retrieve the result from the persisted storage (e.g., S3 bucket in this case). For further details, check out the documented example on persisted results here. Another relevant resource can be found here. If you need more tailored guidance for doing this programmatically, please let me know!
p
@Marvin the task has already been run, I want to use the client to get the flow run results
m
To retrieve the result of a completed flow run using the Prefect client, you can use the following approach:
Copy code
python
from prefect import get_client
from prefect.client.schemas import FlowRun

async def get_flow_run_result(flow_run_id: str):
    async with get_client() as client:
        flow_run = await client.read_flow_run(flow_run_id)
        return flow_run.state.result()

# Example usage
import asyncio
flow_run_id = "your_flow_run_id_here"
flow_run_result = asyncio.run(get_flow_run_result(flow_run_id))
print(flow_run_result)
This code snippet does the following: 1. Establishes a connection with the Prefect client. 2. Reads the flow run using its ID. 3. Retrieves the result from the flow run's state. This will give you the result of the flow run if it was successfully persisted. For further details, you can check the official guide here.