A lightweight Redis-backed task queue built in Python as a learning project toward a full distributed task system.
Building a simple distributed task queue in Python + Redis, with producers, workers, task serialization, and a registry-based execution model.
- Defines serializable tasks with IDs, args/kwargs, and timestamps
- Pushes tasks to Redis lists from a producer
- Pulls tasks from Redis with blocking pop in a worker loop
- Registers callable task handlers and executes them by name
- Includes a local Docker Compose setup for Redis
dtq/task.py: Task model and JSON serialization/deserializationdtq/queue.py: Redis-backed queue client (enqueue,dequeue,length,ping)dtq/worker.py: Task registry, worker loop, task executiontasks.py: Example registered task functionsproducer_main.py: Example producer scriptworker_main.py: Example worker runnerdocker-compose.yml: Local Redis service
- Python 3.10+
- Docker + Docker Compose
- Start Redis:
docker compose up -d- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate- Install dependencies:
pip install -r requirements.txt- Run a worker (terminal 1):
python worker_main.py- Enqueue demo tasks (terminal 2):
python producer_main.pyYou should see the worker pick up and execute tasks from Redis.
TaskQueue reads Redis connection settings from environment variables:
REDIS_HOST(default:localhost)REDIS_PORT(default:6379)
Example:
REDIS_HOST=localhost REDIS_PORT=6379 python worker_main.pyIf a worker thread dies mid-task (machine crash, OOM kill), that task is already gone from the Redis list - BRPOP removed it before we finished processing it. I have to fix this with the RPOPLPUSH reliability pattern - tasks only get removed from the queue after successful completion.