Hi everyone, I'm getting started with Prefect and ...
# best-practices
m
Hi everyone, I'm getting started with Prefect and I am trying to wrap my head around running flows and subflows using deployments. Assume I have a simple code like this:
Copy code
@flow(name="sub-flow")
async def run_subflow():
    print("subflow")

@flow(name="top-flow")
async def run_top():
    print("top flow")
    await run_subflow()
    
async def main_code():
    await run_top()
It runs great locally, but how can I run a top flow on a worker (kubernetes) and subflow on another worker? I have deployments created and I can run it using Kubernetes from Prefect Cloud UI or on schedule, but I just don't know how to run it on worker from code reusing this clean code. Thanks!
In other words, I want this code: • executed on my local machine when testing locally • executed on my worker when running on server How can I achieve that without writing it twice?
h
If you add an
if __name__==‘__main__’: run_top()
block at the bottom, it should execute like any other Python script I think.
m
Thanks @Henning Holgersen Running locally as a Python script works fine. What I wonder about is how to make it execute the flows on the worker (I have the deployment ready)
h
Ah, sorry for the misunderstanding. As far as I know, there is no way to run code that only exists locally on a remote worker. That would be sweet, but would require some hefty engineering. One way or another, your code has to be reachable/adressable from the worker. If you are after the shortest possible development cycle while running code on k8s, I’d suggest looking into github storage. In that case, you can deploy your flow once, and as long as the entrypoint stays the same you are simply a push to main away from running your new code on k8s. There is a prefect cli command to trigger the deployment, so you don’t have to leave your IDE, even though it is not the same as simply running a python script.
m
Thanks again 🙂 By "I have the deployment ready" I meant that I already have the deployment configured and deployed with code in Docker on ECR. It even works when I use "Quick run" in the Prefect Cloud UI. Buuut, how do I run it for real, from code? Let's say I have a web app on the server and I want to have an endpoint that when called, fires
run_top()
as a flow on the worker (instead of in the webapp process). In other words: How do I use my deployment?
d
Same question here! Michał maybe you figured out how to do this?