Performance: Periodic tasks with Celery Beat
MEDIUM IMPACT
This affects backend task scheduling and indirectly impacts frontend user experience by controlling task execution timing and server load.
from celery import Celery from celery.schedules import crontab app = Celery('tasks', broker='redis://localhost') @app.task def my_task(): # task code app.conf.beat_schedule = { 'run-every-minute': { 'task': 'tasks.my_task', 'schedule': crontab(minute='*'), }, } # Run celery beat and worker separately
from celery import Celery app = Celery('tasks', broker='redis://localhost') @app.task def my_task(): # task code # Using a simple while loop in the app to run tasks periodically import time while True: my_task.delay() time.sleep(60)
| Pattern | CPU Usage | Task Scheduling Efficiency | Error Handling | Verdict |
|---|---|---|---|---|
| Manual loop in app | High (blocks main thread) | Inefficient (no retries) | None | [X] Bad |
| Celery Beat scheduler | Low (separate process) | Efficient (cron-like schedules) | Built-in support | [OK] Good |