1.7 KiB
1.7 KiB
PocketFlow BatchNode Example
This example demonstrates the BatchNode concept in PocketFlow by implementing a CSV processor that handles large files by processing them in chunks.
What this Example Demonstrates
- How to use BatchNode to process large inputs in chunks
- The three key methods of BatchNode:
prep: Splits input into chunksexec: Processes each chunk independentlypost: Combines results from all chunks
Project Structure
pocketflow-batch-node/
├── README.md
├── requirements.txt
├── data/
│ └── sales.csv # Sample large CSV file
├── main.py # Entry point
├── flow.py # Flow definition
└── nodes.py # BatchNode implementation
How it Works
The example processes a large CSV file containing sales data:
- Chunking (prep): The CSV file is read and split into chunks of N rows
- Processing (exec): Each chunk is processed to calculate:
- Total sales
- Average sale value
- Number of transactions
- Combining (post): Results from all chunks are aggregated into final statistics
Installation
pip install -r requirements.txt
Usage
python main.py
Sample Output
Processing sales.csv in chunks...
Final Statistics:
- Total Sales: $1,234,567.89
- Average Sale: $123.45
- Total Transactions: 10,000
Key Concepts Illustrated
- Chunk-based Processing: Shows how BatchNode handles large inputs by breaking them into manageable pieces
- Independent Processing: Demonstrates how each chunk is processed separately
- Result Aggregation: Shows how individual results are combined into a final output