Prefect is like a function. It may take inputs and may return an output. Flows may consist of subflows, as well as tasks. Flows can be deployed through the Prefect API for monitoring and scheduling. Something so great about Prefect is that you can easily make a flow by adding @flow
above any function. @flow
is the decorator.
from prefect import flow
@flow
def hello_world():
print("Hello, world!")
To understand a decorator, we must understand that a function in python is a first class object. A first class object in Python can be used and passed as arguments. Furthermore, one can store first class functions in data stuctures and return them from another function. More on first class functions in Python. Below is an example of using a function as a variable in Python.
def add(a,b):
return a + b
def subtract(a,b):
return a - b
# computer is either of the functions above
def compute(a,b,computer):
return computer(a,b)
A decorator is a higher-order function. A higher-order function takes other functions as their input and returns another function. A decorator simply adds extra functionality to an existing function, it decorates it. For Prefect, the @flow
decorator adds functionality we will learn more about, but it includes many of the capabilities such as scheduling, logging, and monitoring. Below is an example of a decorator.
def my_decorator(func):
def wrapper():
print("scheduling this...")
func()
print("logging that...")
return wrapper
@my_decorator
def my_flow():
print("executing stuff")
Decorators are used in many popular libraries, include Prefect above. They are a powerful tool to add functionality to existing code. It is both helpful and rewarding to explore these features and nuances in Python!
Helpful resources: