Pooyan
08/05/2024, 5:01 PMMarvin
08/05/2024, 5:01 PMPooyan
08/05/2024, 6:08 PMMarvin
08/05/2024, 6:08 PMset_flow_run_state
in Prefect, you need to get the session of the flow run you are interested in. You can achieve this by using the get_client()
function to interact with the Prefect API.
Here’s a concise example:
1. Initialize the Client: Use get_client()
to start a session.
2. Retrieve the Flow Run: Fetch the specific flow run you are interested in by its flow_run_id
.
3. Set the Flow State: Use the set_flow_run_state
function to set the desired state.
Sample code:
python
from prefect.client import get_client
from prefect.orion.schemas.states import State
async def set_flow_run_state_example(flow_run_id, new_state: State):
async with get_client() as client:
# Retrieve the flow run details
flow_run = await client.read_flow_run(flow_run_id)
# Set the new state for the flow run
await client.set_flow_run_state(
flow_run_id=flow_run.id,
state=new_state
)
# Example usage
import asyncio
from prefect.orion.schemas.states import Completed
flow_run_id = "your-flow-run-id"
new_state = Completed(message="This flow run has been manually completed.")
asyncio.run(set_flow_run_state_example(flow_run_id, new_state))
### Documentation References
- Interactive Workflows: Guide
- Using the Client: Guide
This setup ensures that you can dynamically manage and modify flow run states as needed within your orchestration logic. If you have further questions or need more specialized assistance, the Prefect community and documentation are great resources.