A high-performance REST API for calculating π (Pi) to arbitrary precision using the Chudnovsky algorithm with Celery for asynchronous task processing.
- Asynchronous Processing: Uses Celery with Redis/RabbitMQ for background Pi calculations
- Progress Tracking: Real-time progress updates during long calculations
- RESTful API: Clean REST API with Swagger documentation
- Performance Prediction: Built-in time estimation using power-law approximation
- High Precision: Supports calculation of π to thousands of decimal places
- Visualization: Generates performance plots and approximation curves
The system consists of:
- Flask REST API: Handles HTTP requests and provides API endpoints
- Celery Workers: Perform heavy Pi calculations in the background
- Redis/RabbitMQ: Message broker for task queuing
- Redis: Result backend for storing calculation results
GET /calculate_pi?n=123
Response (202):
{
"message": "Pi calculation started",
"task_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"decimals": 1000
}POST /check_progress
Request Body:
{
"task_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
}Response:
{
"state": "PROGRESS",
"progress": 0.75,
"task_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"iteration": 750,
"total_iterations": 1000,
"elapsed_time": 15.2,
"result": null
}GET /health
Response:
{
"status": "healthy",
"service": "Pi Calculator API"
}- Python 3.8+
- Redis server
- RabbitMQ (optional, defaults to pyamqp)
- Clone the repository:
git clone <repository-url>
cd PiSelery- Install dependencies:
pip install -r requirements.txt- Start Redis:
redis-server- Start the Celery worker:
celery -A main.celery worker --loglevel=info- Start the Flask API:
python main.pyThe API will be available at http://localhost:5000
CELERY_BROKER_URL: Message broker URL (default:pyamqp://guest@localhost//)CELERY_RESULT_BACKEND: Result backend URL (default:redis://localhost:6379/0)BASE_URL: API base URL for testing (default:http://localhost:5000)
Run the test script to verify API functionality:
python test_api.pyThis will test:
- Health check endpoint
- Pi calculation with different precision levels (10, 50, 10000 decimals)
- Progress tracking during calculations
The implementation uses the Chudnovsky algorithm, one of the fastest methods for calculating π:
π = (426880 * √10005) / Σ
where Σ = Σ_{k=0}^∞ ((-1)^k * (6k)! * (13591409 + 545140134k)) / ((3k)! * (k!)^3 * 640320^{3k + 3/2})
The system uses power-law approximation to estimate calculation time:
T(n) = exp(a) * n^b
Where parameters a and b are learned from previous calculations and stored in approximation_params.json.
PiSelery/
├── main.py # Flask API and Celery configuration
├── pi_calculator.py # Pi calculation algorithms
├── logariphmic_aproximation.py # Performance approximation functions
├── test_api.py # API testing script
├── requirements.txt # Python dependencies
├── approximation_params.json # Pre-trained approximation parameters
└── README.md # This file
Once the server is running, visit http://localhost:5000/docs/ to access interactive Swagger documentation.
import requests
# Start calculation
response = requests.post('http://localhost:5000/calculate_pi',
json={'n': 1000, 'algorithm': 'chudnovsky'})
task_id = response.json()['task_id']
# Check progress
while True:
progress = requests.post('http://localhost:5000/check_progress',
json={'task_id': task_id}).json()
if progress['state'] == 'FINISHED':
print(f"π = {progress['result']}")
break
elif progress['state'] == 'FAILURE':
print(f"Error: {progress['error']}")
break
else:
print(f"Progress: {progress['progress']:.1%}")- Calculation time grows approximately as O(n^1.9) where n is the number of decimal places
- Memory usage scales with the precision required
- The system can handle calculations up to at least 10,000 decimal places efficiently
This project is licensed under the MIT License.