serve method.
Serve a flow
The serve method creates a deployment for the flow and starts a long-running process that monitors for work from the Prefect server. When work is found, it is executed within its own isolated subprocess.hello_world.py
- schedules
- event triggers
- metadata such as tags and description
- default parameter values
Additional serve options
Theserve method on flows exposes many options for the deployment.
Here’s how to use some of those options:
cron: a keyword that allows you to set a cron string schedule for the deployment; see schedules for more advanced scheduling optionstags: a keyword that allows you to tag this deployment and its runs for bookkeeping and filtering purposesdescription: a keyword that allows you to document what this deployment does; by default the description is set from the docstring of the flow function (if documented)version: a keyword that allows you to track changes to your deployment; uses a hash of the file containing the flow by default; popular options include semver tags or git commit hashestriggers: a keyword that allows you to define a set of conditions for when the deployment should run; see triggers for more on Prefect Events concepts
serve() is a long-running processTo execute remotely triggered or scheduled runs, your script with flow.serve must be actively running.
Stop the script with CTRL+C and your schedule will automatically pause.Serve multiple flows at once
Serve multiple flows with the same process using theserve utility along with the to_deployment method of flows:
serve_two_flows.py
- the
flow.to_deploymentinterface exposes the exact same options asflow.serve; this method produces a deployment object - the deployments are only registered with the API once
serve(...)is called - when serving multiple deployments, the only requirement is that they share a Python environment; they can be executed and scheduled independently of each other
- pause and unpause the schedule for the
"sleeper"deployment - use the UI to submit ad-hoc runs for the
"sleeper"deployment with different values forsleep - cancel an active run for the
"sleeper"deployment from the UI
Serve instance methods
You can serve flow methods that are part of a class instance. This is useful when you want to configure a flow once at initialization time and reuse that configuration across all runs.data_processor.py
__init__) is available to all flow runs. This is useful for environment-specific settings, connection parameters, or any configuration that should be consistent across all runs of the deployment.
Retrieve a flow from remote storage
Just like the.deploy method, the flow.from_source method is used to define how to retrieve the flow that you want to serve.
from_source
The flow.from_source method on Flow objects requires a source and an entrypoint.
source
The source of your deployment can be:
- a path to a local directory such as
path/to/a/local/directory - a repository URL such as
https://github.com/org/repo.git - a
GitRepositoryobject that accepts- a repository URL
- a reference to a branch, tag, or commit hash
GitCredentialsfor private repositories
entrypoint
A flow entrypoint is the path to the file where the flow is located within that source, in the form
hello flow from the flows/hello_world.py file in the PrefectHQ/examples repository:
load_from_url.py