Ray-based backend for Django Tasks with distributed execution and database-backed reliability
Project description
django-ray
A Ray-based backend for Django Tasks that enables distributed task execution with database-backed reliability.
Why django-ray?
Django projects often need background task execution. While Celery has been the go-to solution for years, Ray offers a more powerful and flexible approach to distributed computing:
- True distributed computing: Ray was built for distributed workloads from the ground up, not just task queues
- Horizontal scaling: Scale from a single machine to thousands of nodes without changing your code
- Resource-aware scheduling: Request specific CPU, GPU, or memory for tasks
- Actor model support: Maintain stateful workers when needed
- Rich ecosystem: Access to Ray's ML libraries, data processing, and more
Despite Ray's capabilities, there was no straightforward way to use it with Django's built-in Tasks framework. django-ray bridges this gap, letting you leverage Ray's distributed computing power while keeping Django's familiar patterns and database-backed reliability.
Overview
django-ray bridges Django's built-in Tasks framework with Ray's distributed computing capabilities, providing:
- Database-backed reliability: Task state is tracked in your Django database, ensuring no tasks are lost
- Multiple execution modes: Sync, local Ray, Ray cluster, or Ray Job API
- Automatic retries: Failed tasks are retried with exponential backoff
- Admin visibility: Monitor and manage tasks through Django admin
- Graceful shutdown: Workers handle signals properly for clean shutdown
Requirements
- Python 3.12+
- Django 6.0+
- Ray 2.53.0+
Installation
pip install django-ray
Or with uv:
uv add django-ray
Quick Start
- Add
django_rayto yourINSTALLED_APPS:
INSTALLED_APPS = [
# ...
"django_ray",
]
- Configure django-ray settings:
DJANGO_RAY = {
"RAY_ADDRESS": "ray://localhost:10001",
"DEFAULT_CONCURRENCY": 10,
"MAX_TASK_ATTEMPTS": 3,
}
- Run migrations:
python manage.py migrate django_ray
- Start the worker:
# Local Ray (recommended for development)
python manage.py django_ray_worker --queue=default --local
# Connect to Ray cluster
python manage.py django_ray_worker --queue=default --cluster=ray://localhost:10001
# Sync mode (no Ray, for testing)
python manage.py django_ray_worker --queue=default --sync
Worker Execution Modes
| Mode | Flag | Description |
|---|---|---|
| sync | --sync |
Direct execution, no Ray (testing) |
| local | --local |
Local Ray cluster, tasks via @ray.remote |
| cluster | --cluster=<addr> |
Remote Ray cluster, tasks via @ray.remote |
| ray-job | (default) | Ray Job Submission API (process isolation) |
Configuration
| Setting | Default | Description |
|---|---|---|
RAY_ADDRESS |
required | Ray cluster address |
DEFAULT_CONCURRENCY |
10 |
Max concurrent tasks per worker |
MAX_TASK_ATTEMPTS |
3 |
Max retry attempts |
RETRY_BACKOFF_SECONDS |
60 |
Base backoff for retries |
RETRY_EXCEPTION_DENYLIST |
[] |
Exception types that skip auto-retry |
STUCK_TASK_TIMEOUT_SECONDS |
300 |
Timeout before marking tasks as LOST |
Development Setup
Prerequisites
- Python 3.12+
- uv package manager
Installation
git clone <repository-url>
cd django-ray
uv sync
Development Commands
make install # Install dependencies
make format # Format code with Ruff
make lint # Lint code with Ruff
make typecheck # Type check with ty
make test # Run tests
make check # Run lint + typecheck
make ci # Run all CI checks
Django Commands
make migrate # Run migrations
make runserver # Start dev server
make shell # Django shell
make createsuperuser # Create admin user
Worker Commands
make worker # Ray Job API mode
make worker-local # Local Ray (recommended)
make worker-sync # Sync mode (no Ray)
make worker-all # All queues, local Ray
make worker-cluster # Connect to cluster
Quick Start (End-to-End Testing)
Terminal 1 - Start Django server:
make runserver
Terminal 2 - Start worker:
make worker-all
Browser - Test via API:
- Open http://127.0.0.1:8000/api/docs (Swagger UI)
- Try
POST /api/enqueue/add/100/200 - Check
GET /api/executions- see task completed with result300 - View in Admin: http://127.0.0.1:8000/admin/django_ray/raytaskexecution/
Queue Configuration
# Single queue
python manage.py django_ray_worker --queue=default
# Multiple queues
python manage.py django_ray_worker --queue=default,high-priority,low-priority
# All configured queues
python manage.py django_ray_worker --all-queues
Docker
make docker-build
# Run modes:
docker run -p 8000:8000 django-ray:latest web # Production
docker run -p 8000:8000 django-ray:latest web-dev # Development
docker run django-ray:latest worker # Worker (local Ray)
docker run django-ray:latest worker-cluster # Worker (cluster)
Kubernetes Deployment
Deploy using Kustomize manifests in k8s/:
# Build images
make k8s-build
# Deploy
make k8s-deploy
# Check status
make k8s-status
# With TLS enabled
make k8s-gen-tls-certs
make k8s-deploy-tls
See k8s/README.md for detailed deployment documentation.
Project Structure
django-ray/
├── src/django_ray/ # Library source code
│ ├── models.py # RayTaskExecution, TaskWorkerLease
│ ├── admin.py # Admin interface
│ ├── backends.py # Django Task Backend
│ ├── conf/ # Settings
│ ├── runner/ # Task runners
│ │ ├── ray_job.py # Ray Job Submission API
│ │ ├── ray_core.py # Ray Core (@ray.remote)
│ │ ├── leasing.py # Worker coordination
│ │ └── retry.py # Retry logic
│ ├── runtime/ # Task execution
│ │ ├── entrypoint.py # Execution entry point
│ │ ├── distributed.py # parallel_map, scatter_gather
│ │ └── serialization.py
│ └── management/commands/
│ └── django_ray_worker.py
│
├── testproject/ # Example project (development only)
│ ├── api.py # Example REST API
│ ├── tasks.py # Example tasks
│ └── apps/ # Example apps
│
├── tests/ # Test suite
├── docs/ # Documentation
└── k8s/ # Kubernetes manifests
Documentation
Full documentation is available in the docs/ directory:
- Getting Started - Installation and basic setup
- Configuration - All configuration options
- Worker Modes - Execution modes explained
- Tasks - Defining and enqueueing tasks
- Queues - Working with task queues
- Retry & Error Handling - Configuring retries
Deployment
- Kubernetes - Deploy to Kubernetes
- Docker - Running with Docker
- TLS - Securing Ray communication
Reference
- CLI Reference - Command-line options
- Settings Reference - All settings
- API Reference - REST API endpoints
License
This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_ray-0.1.1.tar.gz.
File metadata
- Download URL: django_ray-0.1.1.tar.gz
- Upload date:
- Size: 289.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03d89c2062793480cec4e55c2c025cc6638be6be2da78c7441bc7c2cdd34791a
|
|
| MD5 |
f54459a59fa454d30a45ef2d0eb21256
|
|
| BLAKE2b-256 |
c35c00bcebcc215b1c5775dfd21a0ffc3dc968ad0adefb59da6cf0f92385d07c
|
Provenance
The following attestation bundles were made for django_ray-0.1.1.tar.gz:
Publisher:
release.yml on dariuszpanas/django-ray
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_ray-0.1.1.tar.gz -
Subject digest:
03d89c2062793480cec4e55c2c025cc6638be6be2da78c7441bc7c2cdd34791a - Sigstore transparency entry: 836203704
- Sigstore integration time:
-
Permalink:
dariuszpanas/django-ray@55f4babbfad1905437ad73db4a3419bc6f7e43c1 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/dariuszpanas
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@55f4babbfad1905437ad73db4a3419bc6f7e43c1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_ray-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_ray-0.1.1-py3-none-any.whl
- Upload date:
- Size: 47.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f0ee6eed43bbe13825e0087e23bf101fcadd7bef9c2ad7b65bcd8fa325a496a6
|
|
| MD5 |
382672549a7eabab0cf20180e6925a4c
|
|
| BLAKE2b-256 |
8f51ad382846e11a53777c2dd041f4d285be0e7d3476e75a799b688e1f5de68c
|
Provenance
The following attestation bundles were made for django_ray-0.1.1-py3-none-any.whl:
Publisher:
release.yml on dariuszpanas/django-ray
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_ray-0.1.1-py3-none-any.whl -
Subject digest:
f0ee6eed43bbe13825e0087e23bf101fcadd7bef9c2ad7b65bcd8fa325a496a6 - Sigstore transparency entry: 836203705
- Sigstore integration time:
-
Permalink:
dariuszpanas/django-ray@55f4babbfad1905437ad73db4a3419bc6f7e43c1 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/dariuszpanas
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@55f4babbfad1905437ad73db4a3419bc6f7e43c1 -
Trigger Event:
push
-
Statement type: