Skip to main content

A fast, production-ready task queue for Python

Project description

tarsq

A fast, production-ready task queue for Python backed by Redis.

Features

  • Simple @task decorator to register handlers (sync and async)
  • dispatch() to enqueue jobs from anywhere in your app
  • status() to track job progress
  • Automatic retries (up to 3) with exponential backoff
  • Configurable worker count and task timeout
  • Startup/shutdown lifecycle hooks
  • Crashed worker auto-restart
  • Stuck task recovery on startup

Requirements

  • Python 3.11+
  • Redis

Installation

pip install tarsq

Quick start

1. Define your tasks (myapp/tasks.py):

from tarsq import task

@task("send_email")
def send_email(ctx, payload):
    print(f"Sending email to {payload['to']}")

@task("resize_image")
async def resize_image(ctx, payload):
    print(f"Resizing image at {payload['url']}")

2. Configure the worker (myapp/worker_settings.py):

from tarsq import WorkerSettings

class MyWorkerSettings(WorkerSettings):
    app = "myapp.tasks"
    workers = 5
    timeout = 60

3. Start the worker:

tarsq --settings myapp.worker_settings.MyWorkerSettings

Or without a settings class:

tarsq --app myapp.tasks --workers 5 --timeout 60

4. Dispatch jobs from your application:

from tarsq import dispatch, status

job_id = dispatch("send_email", payload={"to": "user@example.com"})

job = status(job_id)
print(job.status)  # queued | in_progress | completed | failed

Configuration

WorkerSettings

Attribute Type Default Description
app str None Dotted module path containing your @task handlers
workers int 5 Number of concurrent worker threads
timeout int 300 Max seconds a task can run before being killed
ctx dict {} Shared context passed to every task handler
on_startup callable None Called once before workers start (sync or async)
on_shutdown callable None Called once after workers stop (sync or async)

Environment variables

Variable Default Description
REDIS_HOST localhost Redis host
REDIS_PORT 6379 Redis port
REDIS_PASSWORD None Redis password

Variables can also be set in a .env file in your project root.

Lifecycle hooks

from tarsq import WorkerSettings

async def on_startup(ctx):
    ctx["db"] = await create_db_pool()

async def on_shutdown(ctx):
    await ctx["db"].close()

class MyWorkerSettings(WorkerSettings):
    app = "myapp.tasks"
    on_startup = on_startup
    on_shutdown = on_shutdown

The ctx dict is passed into every task handler, making it easy to share connections across tasks.

Job status

from tarsq import status, TaskStatusEnum

job = status(job_id)

job.job_id      # UUID string
job.task        # task name
job.status      # TaskStatusEnum: queued | in_progress | completed | failed
job.retries     # number of retry attempts so far
job.created_at  # ISO 8601 timestamp
job.updated_at  # ISO 8601 timestamp of last status change

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tarsq-0.1.12.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tarsq-0.1.12-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file tarsq-0.1.12.tar.gz.

File metadata

  • Download URL: tarsq-0.1.12.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.4 CPython/3.12.13 Darwin/25.0.0

File hashes

Hashes for tarsq-0.1.12.tar.gz
Algorithm Hash digest
SHA256 605551000dc61f2b523e8ae5edb075aac30a5479165a7fc694e745235b0a5516
MD5 c6a4442f30cd51a7bdcac651febb3ba3
BLAKE2b-256 03fc236df7a857f804ad6c3b539f34d5db926b36d1c370a1620e4d3d26fa61cb

See more details on using hashes here.

File details

Details for the file tarsq-0.1.12-py3-none-any.whl.

File metadata

  • Download URL: tarsq-0.1.12-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.4 CPython/3.12.13 Darwin/25.0.0

File hashes

Hashes for tarsq-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 6b71122af26905f5853374b02c54c5482d447ad90adb42993882c7412e9810c7
MD5 cf2550c3e091ce54a7a1c8ecb3720b88
BLAKE2b-256 13ace6b52c2c1c34e54f45e93e778f5d61605bc99b50da8584ff0ac0fa767399

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page