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.3.0.tar.gz (6.1 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.3.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_tasks_concurrent-0.3.0.tar.gz
  • Upload date:
  • Size: 6.1 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.3.0.tar.gz
Algorithm Hash digest
SHA256 b05222f9982108d681ed2a3dac98d1d78cfaa0a2efe0872cbc107c15a62bbb4a
MD5 60df54e13bd78e916b3624aa24283004
BLAKE2b-256 5e24437efd1f8e8e88cf9e6b55706dec043b1762274db74a987f738773cd6afa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_tasks_concurrent-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a45852fb03a46311ebe1b224e93f476e5671c3f7d5399814212ce1b2f1fd7af5
MD5 7bd64af96ec2ffec8002285d68271d66
BLAKE2b-256 3c22d2a2f0a8eb384dbcc9d5b21bcc2ad798a8ec8b3a416395e680cd685e1eef

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