Skip to main content

Concurrent async worker for Django Tasks (django-tasks)

Project description

django-tasks-concurrent

Concurrent async worker for Django Tasks.

Runs multiple async tasks concurrently using asyncio.TaskGroup. While one task awaits I/O (API calls, database queries), others can execute. Optimized for I/O-bound workloads.

Installation

pip install django-tasks-concurrent

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "django_tasks",
    "django_tasks.backends.database",
    "django_tasks_concurrent",
]

Usage

Run the worker:

# 3 concurrent workers (default)
python manage.py concurrent_worker

# 5 concurrent workers with 0.5s polling interval
python manage.py concurrent_worker --concurrency=5 --interval=0.5

# Specify queue and backend
python manage.py concurrent_worker --queue-name=high-priority --backend=default

Options

Option Default Description
--concurrency 3 Number of concurrent sub-workers
--interval 1.0 Polling interval (seconds) when no tasks
--queue-name settings.TASK_QUEUE_NAME or "default" Queue to process
--backend "default" Django Tasks backend name

How It Works

The worker spawns N sub-workers as asyncio coroutines. Each sub-worker:

  1. Claims a task using SELECT FOR UPDATE SKIP LOCKED
  2. Executes the task (async tasks run natively, sync tasks via thread pool)
  3. Marks task as succeeded/failed
  4. Repeats

This allows true concurrent execution of async tasks - while one awaits an API response, others continue processing.

When to Use

Use concurrent_worker for:

  • I/O-bound tasks (API calls, LLM inference, HTTP requests)
  • Tasks that spend most time awaiting external services
  • Workloads where you want N tasks running simultaneously

Use standard db_worker for:

  • CPU-bound tasks
  • Tasks that don't benefit from async
  • Simple sequential processing

Async Task Example

from django_tasks import task

@task
async def call_llm_api(prompt: str) -> str:
    async with httpx.AsyncClient() as client:
        response = await client.post(
            "https://api.anthropic.com/v1/messages",
            json={"prompt": prompt},
        )
        return response.json()["content"]

With --concurrency=3, three LLM calls can be in-flight simultaneously.

Requirements

  • Python 3.12+
  • Django 6.0+
  • django-tasks 0.5.0+

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

django_tasks_concurrent-0.2.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

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

django_tasks_concurrent-0.2.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file django_tasks_concurrent-0.2.0.tar.gz.

File metadata

  • Download URL: django_tasks_concurrent-0.2.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for django_tasks_concurrent-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d2898cf3b5a84859c495518e62d9af33668f35f72e69ad6a33dd95d1b24b3f65
MD5 3e3e2ec6658edca46b60dfb052936861
BLAKE2b-256 54c474daec964325a73014f712271435ddf4a97bfa894c6f5aade871c616278f

See more details on using hashes here.

File details

Details for the file django_tasks_concurrent-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_tasks_concurrent-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 35327157bd0fddd9ea29242e332b7a617be083561fdac7655776951cbff91dff
MD5 efc9fec82f007b2daf79d234df7caf7d
BLAKE2b-256 2fcdb9162469be4fd52449fdd14ce25e8b51d9ff4173677598dd3046a6546533

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