This tutorial assumes you have already installed Prefect and connected to Prefect Cloud or a self-hosted server instance.
See the prerequisites section of the tutorial for more details.
Flows are like functions.
They can take inputs, perform work, and return an output.
In fact, you can turn any function into a Prefect flow by adding the @flow decorator.
When a function becomes a flow, its behavior changes, giving it the following advantages:
All runs of the flow have persistent state. Transitions between states are recorded, allowing for flow execution to be observed and acted upon.
Input arguments can be type validated as workflow parameters.
Retries can be performed on failure.
Timeouts can be enforced to prevent unintentional, long-running workflows.
Metadata about flow runs, such as run time and final state, is automatically tracked.
They can easily be elevated to a deployment, which exposes a remote API for interacting with it
The simplest way to get started with Prefect is to annotate a Python function with the @flow decorator.
The script below fetches statistics about the main Prefect repository.
Note that httpx is an HTTP client library and a dependency of Prefect.
Let's turn this function into a Prefect flow and run the script:
As with any Python function, you can pass arguments to a flow.
The positional and keyword arguments defined on your flow function are called parameters.
Prefect will automatically perform type conversion using any provided type hints.
Let's make the repository a string parameter with a default value:
Prefect enables you to log a variety of useful information about your flow and task runs, capturing information about your workflows for purposes such as monitoring, troubleshooting, and auditing.
If we navigate to our dashboard and explore the runs we created above, we will notice that the repository statistics are not captured in the flow run logs.
Let's fix that by adding some logging to our flow:
Now the output looks more consistent and, more importantly, our statistics are stored in the Prefect backend and displayed in the UI for this flow run:
The example above is for educational purposes.
In general, it is better to use Prefect artifacts for storing metrics and output.
Logs are best for tracking progress and debugging errors.
So far our script works, but in the future unexpected errors may occur.
For example the GitHub API may be temporarily unavailable or rate limited.
Retries help make our flow more resilient.
Let's add retry functionality to our example above:
As you have seen, adding a flow decorator converts our Python function to a resilient and observable workflow.
In the next section, you'll supercharge this flow by using tasks to break down the workflow's complexity and make it more performant and observable - click here to continue.