Background Work at Scale
Jobs, the scheduler, caching and email โ and how they behave across multiple gunicorn workers.
Jobs, schedules, cache, email
@app.job
def send_receipt(email): ops.send_email(smtp, email, "Receipt", "...")
@app.schedule("0 3 * * *")
def nightly(): OldSession.where(created__lt=cutoff).delete()
@app.cache(ttl=60)
@app.get("/leaderboard")
def board(req): ...The multi-worker gotcha
@app.job and @app.schedule run per gunicorn worker. Two safe patterns: run schedules in one dedicated process, or gate scheduled tasks behind a database lock so only one worker fires them. For heavy volume, swap app.jobs for a Redis-backed queue with the same enqueue() API.
Takeaways
- Jobs/scheduler are per-worker โ plan for it (leader lock or dedicated process).
- Cache read-heavy routes; send email off the request thread.