Prefect Examples
This directory contains example Prefect workflows demonstrating various features and patterns.
Files
numbers.py- A basic workflow with sequential task executionnumbers-concurrent.py- A workflow demonstrating concurrent task execution using.submit()shell-workflow.py- Basic shell command execution using ShellOperationshell-advanced.py- Advanced shell operations with error handling and custom working directoriesnumbers.ipynb- Jupyter notebook version of the sequential workflownumbers-concurrent.ipynb- Jupyter notebook version of the concurrent workflowrequirements.txt- Python dependencies for Prefect examples
Setup
- Install dependencies:
pip install -r requirements.txt (Optional) Configure Prefect profile to use a remote server or Prefect Cloud:
Using Prefect Cloud:
# Sign up at https://app.prefect.cloud if you haven't already prefect cloud login prefect cloud workspace set --workspace <your-workspace-name>Using a custom Prefect server instance:
# Create a new profile prefect profile create <profile-name> prefect profile use <profile-name> # Configure the API URL and key prefect config set PREFECT_API_URL=<your-api-url> prefect config set PREFECT_API_KEY=<your-api-key>Profiles are stored in
~/.prefect/profile.toml. You can switch between profiles using:prefect profile use <profile-name>Using local Prefect server (default):
prefect server startThis will start a local Prefect server and open the UI at
http://localhost:4200.
Running the Examples
Sequential Workflow
Run the basic sequential workflow:
python numbers.py
This example demonstrates:
- Basic task and flow decorators
- Sequential task execution
- Data passing between tasks
- Simple ETL pipeline pattern
Concurrent Workflow
Run the concurrent workflow:
python numbers-concurrent.py
This example demonstrates:
- Concurrent task execution using
.submit() - Parallel execution of independent tasks
- Using Prefect futures for async task management
- Flow with
log_prints=Truefor better observability
Shell Command Execution
Run the basic shell workflow:
python shell-workflow.py
This example demonstrates:
- Executing shell commands using
ShellOperation - Processing files with external tools (e.g.,
wc) - Running multiple commands in sequence
- Using environment variables in shell commands
Run the advanced shell workflow:
python shell-advanced.py
This example demonstrates:
- Custom working directories
- Error handling with
continue_on_error - Capturing both stdout and stderr with
return_all - Chaining multiple commands with proper error handling
Understanding the Examples
Sequential Execution (numbers.py)
Tasks execute in order based on their dependencies:
get_number()tasks run sequentiallyadd()waits for two numbersmultiply()waits for the summean()calculates the mean of all numbers
Concurrent Execution (numbers-concurrent.py)
Independent tasks can run in parallel:
- Multiple
get_number()tasks can run simultaneously - Tasks are submitted using
.submit()which returns futures - Prefect automatically manages task dependencies and parallelization
Working with Jupyter Notebooks
If you prefer to work with notebooks:
- Open
numbers.ipynbornumbers-concurrent.ipynbin Jupyter - Install the kernel dependencies:
pip install -r requirements.txt - Run the cells to execute the workflow
Monitoring Workflows
Local Prefect Server
Start the local server to monitor workflow execution:
prefect server start
The UI provides:
- Workflow run history
- Task execution logs
- Visual representation of task dependencies
- Performance metrics
Prefect Cloud
For cloud-based monitoring:
- Sign up at app.prefect.cloud
- Authenticate:
prefect cloud login - Set your workspace:
prefect cloud workspace set --workspace <your-workspace>
Additional Resources
For comprehensive documentation, tutorials, and additional resources, see the Prefect documentation page.