pip install flask celery redis
Celery requires a message broker. We’ll use Redis, so install it:
sudo apt install redis # Linux
brew install redis # macOS
Start the Redis server:
redis-server
app.py
from flask import Flask, request, jsonify
from celery import Celery
app = Flask(__name__)
app.config["CELERY_BROKER_URL"] = "redis://localhost:6379/0"
app.config["CELERY_RESULT_BACKEND"] = "redis://localhost:6379/0"
celery = Celery(app.name, broker=app.config["CELERY_BROKER_URL"])
celery.conf.update(app.config)
@celery.task
def long_task(n):
import time
time.sleep(n) # Simulate long-running task
return f"Task completed after {n} seconds!"
@app.route("/run-task", methods=["POST"])
def run_task():
data = request.get_json()
task = long_task.apply_async(args=[data.get("time", 5)])
return jsonify({"task_id": task.id}), 202
@app.route("/task-status/<task_id>")
def task_status(task_id):
task = long_task.AsyncResult(task_id)
return jsonify({"status": task.status, "result": task.result})
if __name__ == "__main__":
app.run(debug=True)
1️⃣ Run Flask:
python app.py
2️⃣ Start the Celery Worker:
celery -A app.celery worker --loglevel=info
Send a request using Postman or cURL:
curl -X POST http://127.0.0.1:5000/run-task -H "Content-Type: application/json" -d '{"time": 10}'
This will return a task ID:
{"task_id": "d4f3b9c7-XXXX-XXXX-XXXX-XXXXXXXXXXXX"}
For production, use Celery with a process manager like Supervisor to keep it running:
sudo apt install supervisor
Then, create a config file for Celery:
[program:celery]
command=celery -A app.celery worker --loglevel=info
autostart=true
autorestart=true
stderr_logfile=/var/log/celery.err.log
stdout_logfile=/var/log/celery.out.log
In this guide, we covered:
✅ Installing and setting up Celery with Flask.
✅ Running background tasks without blocking Flask.
✅ Using Redis as a message broker.
✅ Deploying Celery for production-ready applications.
Now, your Flask app can efficiently handle long-running tasks like email notifications, data processing, and more! 🚀