AMQP support for django. Supporting RabbitMQ and Azure ServiceBus.
Project description
Django AMQP
AMQP support for Django, with implementations for RabbitMQ and Azure ServiceBus.
Overview
Django AMQP is an extension for Django that provides Advanced Message Queuing Protocol (AMQP) support. It enables you to easily implement message queues and task processing in your Django applications using either RabbitMQ or Azure Service Bus as the backend.
The library integrates with Django's task framework, allowing you to define, queue, and process asynchronous tasks with support for scheduled/deferred execution.
Installation
pip install django-amqp
OR
uv add django-amqp
Configuration
Add django_amqp to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
# ...
'django_amqp',
# ...
]
Configure your task backend in settings.py:
SERVICEBUS_CONNECTION_STRING = os.environ.get("SERVICEBUS_CONNECTION_STRING")
TASKS = {
"default": {
"BACKEND": "django_amqp.backend.ServiceBusBackend",
"QUEUES": [
"default",
],
"OPTIONS": {"connection_string": SERVICEBUS_CONNECTION_STRING},
}
}
Usage
Defining Tasks
Create a task using Django's task decorator:
from django.tasks import task
@task(queue_name="my-queue")
def my_background_task(param1, param2, **kwargs):
# Your task logic here
pass
NOTE:
queue_namedefaults to"default"(as per django tasks implementation). You may want to name your main worker ServiceBus/RabbitMQ queue as"default"to match this. Otherwise, specifyqueue_namefor each task.*
Queueing Tasks
Queue a Task for Immediate Execution
from myapp.tasks import my_background_task
# Queue for immediate execution
my_background_task.enqueue(param1="value1", param2="value2")
you can also batch queue
from myapp.tasks import my_background_task
# Batch Queue for immediate execution
my_background_task.get_backend().batch_enqueue(
my_background_task, [
([], {param1="value1", param2="value2"}),
([], {param1="value3", param2="value4"})
]
)
# It's more performant to queue many messages at once, instead of making a connection
# for each
Queue and Cancel a Task for Delayed Execution
You can cancel scheduled messages using the sequence number returned when scheduling a message:
from django_amqp.service_bus import cancel_deferred_message
# Schedule a task and get the sequence number
sequence_number = my_background_task.delay_until(future_time, param1="value1")
# Later, if you need to cancel it
cancel_deferred_message(sequence_number, queue_name="my-queue")
Running Workers
To process messages from a specific queue:
python manage.py amqp_worker --queue-name="my-queue" --burst
The --burst flag tells the worker to process all available messages and then exit.
Error Handling
When using the ServiceBus Backend, failed tasks are automatically moved to the dead-letter queue with the error reason and description. You can implement additional error handling in your task functions.
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_amqp-0.1.1a1.tar.gz.
File metadata
- Download URL: django_amqp-0.1.1a1.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e399231aa1e433728dd9f6c0c8a648f936bf4797a8f7643479687488f9f83d86
|
|
| MD5 |
e5f983fd713dbacf2f7d7fbc42c1f87e
|
|
| BLAKE2b-256 |
5a8b4709078d8426a40ce580541c0f2aa291fbf63b80564ec37d52296ea6596d
|
File details
Details for the file django_amqp-0.1.1a1-py3-none-any.whl.
File metadata
- Download URL: django_amqp-0.1.1a1-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51f0206e211255b6d7a4ae921b958eadca5c3aedda89e3944008e908aa7f127b
|
|
| MD5 |
092bffec4ce9adf7e2bc0099ec9d6c47
|
|
| BLAKE2b-256 |
8b25f1c72057a61ade8b9a8f96b4fa8198f54080510c6a78a242aa14c301d021
|