https://prefect.io logo
Title
r

Raviraja Ganta

05/12/2022, 3:07 PM
Is there any example code available for calling flows via some python API (flask / fastapi).
k

Kevin Kho

05/12/2022, 3:09 PM
Are you using Prefect 1 or 2? You can hit the API for both to trigger flow runs
r

Raviraja Ganta

05/12/2022, 3:12 PM
I am using Prefect 1
I am getting
ERROR:prefect.TaskRunner:Unexpected error: OSError(24, 'Too many open files')
error when using Flask API
k

Kevin Kho

05/12/2022, 3:19 PM
You are trying to use
flow.run()
with an API? I think it will be easier if you trigger a Prefect Cloud Flow Run
r

Raviraja Ganta

05/12/2022, 3:21 PM
I am trying to test locally. That's why using that. If I run the flow code as it is, it is working fine. But when ran with flask it is throwing this error. How to trigger prefect cloud flow run?
k

Kevin Kho

05/12/2022, 3:26 PM
You can use the
Client.create_flow_run
r

Raviraja Ganta

05/12/2022, 3:42 PM
Okay will try that. Let me know if there is any work around with running locally
@Anna Geller this is the use case for using client. I am just trying the
get_agent_config
method to test the client. When I use it for triggering the flow it worked.
a

Anna Geller

05/13/2022, 1:07 PM
I see - but there is no need to do that - you can use the create_flow_run_task directly in your flow without client and without agent config:
from prefect import Flow
from prefect.tasks.prefect import create_flow_run, wait_for_flow_run

PROJECT_NAME = "community"


with Flow("sample_flow") as flow:
    child_run_id = create_flow_run(
        flow_name="child_flow",
        project_name=PROJECT_NAME,
        task_args={"name": "Friendly name for a DAG node"},
    )
    extract_load_wait_task = wait_for_flow_run(
        child_run_id,
        raise_final_state=True,
        stream_logs=True,
        task_args={"name": "Friendly wait task name"},
    )