Last modified: Mar 27, 2025 By Alexander Williams
Install RQ (Redis Queue) in Python Step by Step
RQ (Redis Queue) is a simple Python library for queueing jobs and processing them in the background with workers. It uses Redis as a backend.
This guide will walk you through installing and setting up RQ in Python.
Prerequisites
Before installing RQ, ensure you have the following:
- Python 3.6 or higher installed.
- Redis server installed and running.
- Basic knowledge of Python and Redis.
Step 1: Install Redis
RQ requires Redis to work. Install Redis on your system first.
For Ubuntu/Debian:
sudo apt-get install redis-server
For macOS (using Homebrew):
brew install redis
Start the Redis server:
redis-server
Step 2: Install RQ
Install RQ using pip, Python's package manager.
pip install rq
If you encounter a ModuleNotFoundError
, check out our guide on how to solve ModuleNotFoundError.
Step 3: Create a Simple RQ Job
Create a Python script to define a job. Save it as job.py
.
def add_numbers(a, b):
return a + b
Step 4: Enqueue the Job
Create another script to enqueue the job. Save it as enqueue.py
.
from redis import Redis
from rq import Queue
from job import add_numbers
# Connect to Redis
redis_conn = Redis()
# Create a queue
q = Queue(connection=redis_conn)
# Enqueue the job
job = q.enqueue(add_numbers, 5, 7)
print(f"Job ID: {job.id}")
Step 5: Start an RQ Worker
Run an RQ worker to process the job. Open a new terminal and run:
rq worker
The worker will start and process the job.
Step 6: Check the Result
Create a script to check the job result. Save it as result.py
.
from redis import Redis
from rq import Queue
# Connect to Redis
redis_conn = Redis()
# Create a queue
q = Queue(connection=redis_conn)
# Fetch the job
job = q.fetch_job("YOUR_JOB_ID") # Replace with your job ID
# Print the result
print(f"Result: {job.result}")
Conclusion
You have successfully installed and set up RQ in Python. RQ is a powerful tool for background job processing.
With Redis as the backend, it ensures reliability and scalability. Try experimenting with more complex jobs.
For more Python tips, explore our other tutorials.