Claude Code for RQ: Redis Queue Background Tasks in Python — Claude Skills 360 Blog
Blog / AI / Claude Code for RQ: Redis Queue Background Tasks in Python
AI

Claude Code for RQ: Redis Queue Background Tasks in Python

Published: May 10, 2028
Read time: 5 min read
By: Claude Skills 360

RQ (Redis Queue) processes background jobs with Redis. pip install rq. Connect: from redis import Redis; from rq import Queue; redis = Redis(); q = Queue(connection=redis). Enqueue: job = q.enqueue(my_func, arg1, arg2). Named queue: Queue("emails", connection=redis). Job id: job.id. Status: job.get_status(). Result: job.result (after completion). Refresh: job.refresh(). Fetch: from rq.job import Job; Job.fetch(job_id, connection=redis). Worker CLI: rq worker — processes default queue. Multiple: rq worker emails high default. Retry: q.enqueue(fn, retry=Retry(max=3, interval=[10, 30, 60])). Timeout: q.enqueue(fn, job_timeout=120). TTL: q.enqueue(fn, result_ttl=3600). At: from rq.job import Callback; q.enqueue_at(datetime(2024,4,1,9), fn). In: q.enqueue_in(timedelta(minutes=5), fn). Depends on: q.enqueue(fn2, depends_on=job1). On success: q.enqueue(fn, on_success=Callback(success_fn)). Failed: from rq import FailedJobRegistry; FailedJobRegistry(queue=q). job.exc_info — exception traceback. Cancel: job.cancel(). Delete: job.delete(). Scheduler: pip install rq-scheduler. from rqscheduler import Scheduler. scheduler.enqueue_in(timedelta(hours=1), fn). scheduler.cron("0 9 * * *", fn). Simple worker (sync): from rq import SimpleWorker. Claude Code generates RQ task queues, worker pools, job pipelines, and FastAPI background task systems.

CLAUDE.md for RQ

## RQ Stack
- Version: rq >= 1.16 | pip install rq
- Queue: from rq import Queue; q = Queue(connection=Redis())
- Enqueue: job = q.enqueue(fn, *args, job_timeout=120, result_ttl=3600)
- Retry: q.enqueue(fn, retry=Retry(max=3, interval=[10, 30, 60]))
- Worker: rq worker [queue_names] | SimpleWorker for in-process testing
- Monitor: job.get_status() | Job.fetch(id, conn) | FailedJobRegistry

RQ Background Task Pipeline

# app/tasks.py — RQ Queue, enqueue, retry, callbacks, job tracking, and FastAPI
from __future__ import annotations

import time
from datetime import timedelta
from typing import Any, Callable

from redis import Redis
from rq import Queue, Retry
from rq.job import Job, Callback
from rq.registry import FailedJobRegistry, StartedJobRegistry
from rq.worker import SimpleWorker


# ─────────────────────────────────────────────────────────────────────────────
# 1. Connection and queue factory
# ─────────────────────────────────────────────────────────────────────────────

def make_redis(
    host: str = "localhost",
    port: int = 6379,
    db: int = 0,
    password: str | None = None,
    decode_responses: bool = False,
) -> Redis:
    """Create a Redis connection for RQ."""
    return Redis(host=host, port=port, db=db, password=password,
                 decode_responses=decode_responses)


def make_queue(
    name: str = "default",
    redis: Redis | None = None,
    host: str = "localhost",
    port: int = 6379,
    is_async: bool = True,
) -> Queue:
    """
    Create an RQ Queue.
    is_async=False: execute jobs synchronously (for testing without Redis).

    Example:
        q     = make_queue("emails")
        q_low = make_queue("low")
        q_test = make_queue(is_async=False)   # no Redis needed
    """
    conn = redis or make_redis(host=host, port=port)
    return Queue(name, connection=conn, is_async=is_async)


# ─────────────────────────────────────────────────────────────────────────────
# 2. Enqueue helpers
# ─────────────────────────────────────────────────────────────────────────────

def enqueue(
    q: Queue,
    fn: Callable,
    *args: Any,
    timeout: int = 180,
    result_ttl: int = 3600,
    failure_ttl: int = 86400,
    retry_max: int = 0,
    retry_intervals: list[int] | None = None,
    description: str | None = None,
    on_success: Callback | None = None,
    on_failure: Callback | None = None,
    **kwargs: Any,
) -> Job:
    """
    Enqueue a function for background execution.

    timeout: max seconds the job may run (default 3 min).
    result_ttl: seconds to keep result in Redis after completion.
    failure_ttl: seconds to keep failed job info.
    retry_max: number of automatic retries on failure.
    retry_intervals: backoff seconds between retries (default [10, 30, 60]).

    Example:
        job = enqueue(emails_q, send_welcome_email, user_id=42, timeout=30)
        print(job.id)
    """
    kw: dict = {
        "job_timeout":  timeout,
        "result_ttl":   result_ttl,
        "failure_ttl":  failure_ttl,
    }
    if retry_max > 0:
        kw["retry"] = Retry(max=retry_max, interval=retry_intervals or [10, 30, 60])
    if description:
        kw["job_description"] = description
    if on_success:
        kw["on_success"] = on_success
    if on_failure:
        kw["on_failure"] = on_failure

    return q.enqueue(fn, *args, **{**kw, **kwargs})


def enqueue_in(
    q: Queue,
    delay: timedelta,
    fn: Callable,
    *args: Any,
    **kwargs: Any,
) -> Job:
    """
    Enqueue a job to run after a delay.
    Requires: rq >= 1.10 for built-in enqueue_in support.
    """
    return q.enqueue_in(delay, fn, *args, **kwargs)


def enqueue_chain(
    q: Queue,
    steps: list[tuple[Callable, tuple, dict]],
    **enqueue_kwargs: Any,
) -> list[Job]:
    """
    Enqueue a chain of dependent jobs (step N depends on step N-1).
    steps: [(fn, args, kwargs), ...]
    Returns list of Job objects.

    Example:
        jobs = enqueue_chain(q, [
            (extract_data,  (url,),    {}),
            (transform_data, (),       {}),
            (load_data,      (db_url,), {}),
        ])
    """
    jobs: list[Job] = []
    for i, (fn, args, kw) in enumerate(steps):
        depends_on = jobs[-1] if jobs else None
        job_kw = dict(enqueue_kwargs)
        if depends_on:
            job_kw["depends_on"] = depends_on
        job = q.enqueue(fn, *args, **{**job_kw, **kw})
        jobs.append(job)
    return jobs


# ─────────────────────────────────────────────────────────────────────────────
# 3. Job monitoring
# ─────────────────────────────────────────────────────────────────────────────

def get_job(job_id: str, redis: Redis) -> Job | None:
    """Fetch a job by ID. Returns None if not found."""
    try:
        return Job.fetch(job_id, connection=redis)
    except Exception:
        return None


def job_status(job_id: str, redis: Redis) -> dict[str, Any]:
    """
    Return a status dict for a job.
    status: "queued" | "started" | "finished" | "failed" | "stopped" | "canceled"
    """
    job = get_job(job_id, redis)
    if job is None:
        return {"id": job_id, "status": "not_found"}

    job.refresh()
    return {
        "id":          job.id,
        "status":      job.get_status().value,
        "result":      job.result,
        "description": job.description,
        "enqueued_at": str(job.enqueued_at),
        "started_at":  str(job.started_at),
        "ended_at":    str(job.ended_at),
        "exc_info":    job.exc_info,
    }


def wait_for_job(
    job: Job,
    poll_interval: float = 0.5,
    timeout: float = 30.0,
) -> Any:
    """
    Poll until a job finishes, then return its result.
    Raises TimeoutError if it doesn't complete within timeout seconds.
    Raises RuntimeError if the job failed.
    """
    start = time.monotonic()
    while True:
        job.refresh()
        status = job.get_status().value
        if status == "finished":
            return job.result
        if status == "failed":
            raise RuntimeError(f"Job {job.id} failed: {job.exc_info}")
        if time.monotonic() - start > timeout:
            raise TimeoutError(f"Job {job.id} timed out after {timeout}s")
        time.sleep(poll_interval)


# ─────────────────────────────────────────────────────────────────────────────
# 4. Registry helpers
# ─────────────────────────────────────────────────────────────────────────────

def failed_jobs(q: Queue, count: int = 10) -> list[dict]:
    """Return info on the most recent failed jobs."""
    registry = FailedJobRegistry(queue=q)
    conn = q.connection
    result = []
    for job_id in registry.get_job_ids()[:count]:
        job = get_job(job_id, conn)
        if job:
            result.append({
                "id":       job.id,
                "func":     job.func_name,
                "exc_info": str(job.exc_info)[:200],
            })
    return result


def requeue_failed(q: Queue, max_jobs: int = 50) -> int:
    """Re-enqueue all failed jobs. Returns count re-queued."""
    registry = FailedJobRegistry(queue=q)
    count = 0
    for job_id in registry.get_job_ids()[:max_jobs]:
        try:
            registry.requeue(job_id)
            count += 1
        except Exception:
            pass
    return count


# ─────────────────────────────────────────────────────────────────────────────
# 5. Example tasks (define in separate module for worker imports)
# ─────────────────────────────────────────────────────────────────────────────

def send_email(to: str, subject: str, body: str) -> dict:
    """Example task: simulate sending an email."""
    time.sleep(0.1)  # simulate network call
    return {"status": "sent", "to": to, "subject": subject}


def process_image(image_url: str, size: tuple[int, int] = (128, 128)) -> dict:
    """Example task: simulate image processing."""
    time.sleep(0.2)
    return {"url": image_url, "size": size, "status": "processed"}


def generate_report(report_id: int, format: str = "pdf") -> dict:
    """Example task: simulate report generation."""
    time.sleep(0.5)
    return {"report_id": report_id, "format": format, "url": f"/reports/{report_id}.{format}"}


# ─────────────────────────────────────────────────────────────────────────────
# 6. FastAPI integration
# ─────────────────────────────────────────────────────────────────────────────

FASTAPI_EXAMPLE = '''
from fastapi import FastAPI, BackgroundTasks, Depends
from rq import Queue
from redis import Redis
from app.tasks import enqueue, job_status, send_email, generate_report

app = FastAPI()

_redis = Redis()
_queue = Queue("emails", connection=_redis)
_report_queue = Queue("reports", connection=_redis)

def get_queue() -> Queue:
    return _queue

@app.post("/send-email/")
def trigger_email(to: str, subject: str, body: str, q: Queue = Depends(get_queue)):
    job = enqueue(q, send_email, to, subject, body, timeout=30)
    return {"job_id": job.id, "status": "queued"}

@app.get("/jobs/{job_id}")
def check_job(job_id: str):
    return job_status(job_id, _redis)

@app.post("/reports/")
def create_report(report_id: int, format: str = "pdf"):
    job = enqueue(_report_queue, generate_report, report_id, format=format, timeout=120)
    return {"job_id": job.id}
'''


# ─────────────────────────────────────────────────────────────────────────────
# Demo (sync mode — no Redis needed)
# ─────────────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    print("=== RQ in sync mode (no Redis) ===")
    q = make_queue(is_async=False)  # jobs run immediately in-process

    job = enqueue(q, send_email, "[email protected]", "Welcome!", "Hello Alice")
    print(f"  Job {job.id}: {job.get_status()}")
    print(f"  Result: {job.result}")

    job2 = enqueue(q, generate_report, 42, format="pdf", timeout=60)
    print(f"  Report job {job2.id}: {job2.result}")

    print("\n=== Job chain ===")
    jobs = enqueue_chain(q, [
        (send_email,     ("[email protected]", "Step 1", "body"), {}),
        (generate_report, (99,),                         {"format": "xlsx"}),
    ])
    print(f"  Chain jobs: {[j.id for j in jobs]}")
    for j in jobs:
        print(f"    {j.id}: {j.result}")

For the Celery alternative — Celery supports multiple brokers (Redis, RabbitMQ, SQS), task routing, canvas primitives (chord, chain, group), and periodic tasks via Celery Beat — it is the enterprise choice for complex task graphs; RQ is simpler (Redis-only, pure Python, zero configuration) and is the right choice for most web apps where you want background jobs without Celery’s complexity and configuration surface area. For the dramatiq alternative — dramatiq uses actor-based message passing with retry/rate-limiting middleware and supports RabbitMQ and Redis; RQ’s API is more Pythonic (q.enqueue(fn, arg)) and comes with a built-in web dashboard (rq-dashboard), making it faster to get started while still handling retries, job chaining, and failure registries. The Claude Skills 360 bundle includes RQ skill sets covering make_redis()/make_queue() factory, enqueue() with timeout/retry/callbacks, enqueue_in() delayed execution, enqueue_chain() dependent pipelines, job_status()/wait_for_job() monitoring, failed_jobs()/requeue_failed() registry helpers, send_email/process_image/generate_report example tasks, and FastAPI Depends(get_queue) integration. Start with the free tier to try Redis Queue background task code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free