Eric
09/01/2023, 11:27 PMif __name__ == "__main__":
deployment: Deployment = Deployment.build_from_flow(
flow=test_flow,
name="example",
version="1",
tags=["demo"],
work_pool_name='local_agent_work_pool'
)
deployment.apply()
I confirmed that the deployment worked in the Prefect Cloud UI.
In my flask endpoint, I am trying to submit the flow run to Prefect cloud like so, but it feels wrong:
@flask_route.route("/prefect", methods=['POST'])
def start_prefect_flow():
# prefect_client = get_client(
# # api_key='redacted'
# )
prefect_client = PrefectClient(
api='redacted',
api_key='redacted'
)
res = prefect_client.create_flow_run(test_flow)
print(res)
First, I don't think I should be creating the client this way, but unsure how else I should be passing my api/api key information to it. This is also returning a coroutine object instead of what I thought would be information about my flow run, but maybe that makes sense since I don't see a flow run being kicked off. Some assistance here would be super helpful!Nate
09/03/2023, 1:50 AMrun_deployment
is the easiest way to do this if you want to use the SDK
from prefect.client.schemas.core import FlowRun
from prefect.deployments import run_deployment
# blocks until flow run completes unless you call with `timeout=0`
flow_run: FlowRun = run_deployment("test-flow/test-deployment", params=dict(...))
and if you're just getting started I'd recommend using:
• our flow.serve deployment UX if you want a pythonic deployment experience
• our worker / prefect.yaml
deployment UX
instead of Deployment.build_from_flow
- because build_from_flow
is meant to create block-based deployments that work with agents (not workers) and do not leverage things like pull
steps etc, and for deployments with dynamically created infra, workers are our main recommendationEric
09/03/2023, 2:59 PMNate
09/03/2023, 8:39 PMPREFECT_API_KEY
and PREFECT_API_URL
set as env vars, so commonly:
• your local machine at deployment time (or github actions env at deployment time)
• the machine running your worker (e.g. put it in your values.yaml
if using the helm chart)
Running async jobs from an api server seems like a very common case people would want to implementagreed! i think some official examples of this would be helpful, but using
run_deployment
shouldnt be meaningfully different from any simple flask / fastapi endpoint, for example
from uuid import UUID
from fastapi import FastAPI, Body
from prefect.deployments import run_deployment
app = FastAPI()
@app.post("/run-prefect-flow/")
async def run_prefect_deployment(slug: str = Body(...), params: dict = Body(...)) -> UUID:
# timeout = 0 to return immediately instead of blocking
flow_run = await run_deployment(slug, parameters=params, timeout=0)
return flow_run.id
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
which could be invoked like
curl -X 'POST' \
-H 'Content-Type: application/json' \
--data '{"slug": "foo/some-deployment", "params": {"key": "value"}}' \
'<http://localhost:8000/run-prefect-flow/>'
note that my foo/some-deployment
is assumed to already exist and **params
should match the signature of that deployed flowEric
09/04/2023, 3:08 AMNate
09/04/2023, 4:04 PMwhen it is recommended to deploy the flow, how to pass the deployment uuid to the flask app in prodthese sound a bit like implementation details of your use case that'd be hard to recommend from the outside. happy to discuss further if you have specific questions - that or you could speak with our pro-serv team if that's something you're interested in