Docs / Guides / Background jobs

Platform 04 · Study guide

Background jobs

Move slow work off the request thread with sync, database, or Redis queue drivers.

What you will learn
  • How to define a Job class
  • How to push work and run a worker
  • Which driver to pick (sync / database / redis)

1. Setup

from tpy.queue import Job, Queue, SyncQueueDriver

class SendWelcomeEmail(Job):
    def __init__(self, user_id: str) -> None:
        self.user_id = user_id

    def handle(self) -> None:
        # send email here
        print("welcome", self.user_id)

# Development: run inline in the same process
Queue(SyncQueueDriver()).push(SendWelcomeEmail("u1"))

2. Database driver (typical local/prod without Redis)

$ tpy queue table
$ tpy queue work --driver database --once
$ tpy queue work --driver database

queue table creates _tpy_jobs (and failed-job storage). Workers pull jobs and call handle().

3. Redis driver

$ pip install "tamilPY[redis]"
$ tpy queue work --driver redis --redis-url redis://localhost:6379/0

4. How it works

  1. Your app code push()es a Job instance onto a Queue.
  2. The driver serializes and stores it (memory/inline, SQL table, or Redis list).
  3. A worker process pops jobs and executes handle().
  4. Failures can land in a failed-jobs store depending on driver.
Use SyncQueueDriver in tests. Use database/redis workers in real deployments so HTTP requests stay fast.