https://prefect.io logo
z

Zachary Loertscher

08/14/2023, 7:28 PM
Is it possible to use
.with_options()
on a flow deployment, without using a sub-flow to capture the
.with_options()
values? i.e. the following
.with_options()
works locally, but not after I deploy it (running the deployment completely ignores the
.with_options()
parameters):
Copy code
@flow
def my_test_flow():
    print('hello')

if __name__ == 'main':
    my_test_flow.with_options(name='some other name')()
The following uses a subflow to capture the options before running the flow. It works, but do I HAVE to do it this way?
Copy code
@flow
def my_test_flow():
    print('hello')

@flow
def main_flow():
    my_test_flow().with_options(name='some other name')()

if __name__ == 'main':
    main_flow()
I'd typically use the
@flow
decorator instead, but I need to use
.with_options
so that I can utilize some parameter over-loads from my
--param
arguments for
prefect deployment build
directly on the flow
n

Nate

08/14/2023, 7:43 PM
hi @Zachary Loertscher - this comes down to the difference between a
Flow
and a
Deployment
with_options
is a class method on a
Flow
, which means it takes the existing instance of a flow and returns a modified version of it (with your setting overrides) - this flow instance knows nothing about any
Deployment
that may refer to it (as intended) if you're running a pre-defined deployment, you'll want to use `run_deployment`(docs) and provide any overrides like
parameters
or
flow_run_name
as kwargs there
z

Zachary Loertscher

08/14/2023, 7:53 PM
Thanks for this great information! is
run_deployment
something you would use like this?
Copy code
@flow
def my_test_flow():
    print('hello')

if __name__ == "main":
    run_deployment()
where I was previously doing...
Copy code
@flow
def my_test_flow():
    print('hello')

if __name__ == "main":
    my_test_flow()
Just trying to understand how it fits in - it seems like it would replace calling a specific flow
n

Nate

08/14/2023, 7:58 PM
it seems like it would replace calling a specific flow
in a sense - yes!
run_deployment
will trigger a flow run from a deployed flow. so whatever you see when you do
Copy code
prefect deployment ls
can be triggered with
run_deployment
. here's a (somewhat involved) example using
run_deployment
also to note about
run_deployment
, if you call it within a flow like
Copy code
@flow
def parent():
   flow_run = run_deployment("foo-flow/foo-deployment", parameters={"bar": 42})
   assert flow_run.state.is_completed()
   assert flow_run.state.result() == 42 # assume foo-flow literally just returns bar
then you'll see that this run of
foo-flow/foo-deployment
is treated as a subflow run of
parent
but it doesnt need to be called from within a flow, you can call it from wherever you have prefect
z

Zachary Loertscher

08/14/2023, 8:06 PM
Interesting - I will have to dig deeper into this option!! Looks like I would still be using a sub-flow if I want to run my deployment from prefect cloud. I'd have to call
run_deployment
from a separate process, right?
n

Nate

08/14/2023, 8:09 PM
by
if I want to run my deployment from prefect cloud
do you mean basically clicking "Custom Run" in the UI on a deployment but only programmatically? if so, yes you'd have to call
run_deployment
from somewhere
z

Zachary Loertscher

08/14/2023, 8:11 PM
Yep - just because we have our CRON's set up in prefect cloud so that's what would be calling the deployment, which calls the flow, etc.
n

Nate

08/14/2023, 8:13 PM
ah if you have a cron schedule attached to your deployment already that youd like to use to trigger your deployment, then you wouldnt need to call it otherwise run_deployment is for the case where you want to programmatically trigger an adhoc run of a deployment (with custom params if you so choose)
thank you 1
z

Zachary Loertscher

08/14/2023, 8:19 PM
This all makes sense - thank you so much for answering my questions! I'll continue down the route I currently have, then 🙂
n

Nate

08/14/2023, 8:19 PM
cool - no problem!