ModelQueue: Django task queue based on Django models
Published on Aug. 22, 2023, 12:12 p.m.
“You have effectively ignored most of what’s important in queuesing theory.”.
ModelQueue is a task queue based on Django models.ModelQueue is dangerous project .It takes a bad idea and makes it easy to use.You may regret using your database as a task list but it won’t be today!
Features include:
- Pure-Python
- Supports the admin interface of Django.
- Tasks can be aborted, and canceled .
- Supports multiple tries per task.
- Bring your own parallelism with threading.
- Performance matters
- Fully documented and documented.
- Compatible with all versions of Django.
- It was tested on Linux, Mac OS X and Windows.
The installation of ModelQueue is simple with pip :
$ python -m pip install modelqueue
You can access documentation in the interpreter using the built-in help function :
import modelqueue
help(modelqueue)
This part of the documentation describes introduction, benchmarks, development, andAPI.
ModelQueue Documentation
Quickstart
For example, in appname/models.py:
import modelqueue
from django.db import models
class Task(models.Model):
data = models.TextField()
status = modelqueue.StatusField(
# ^-- Just a models.BigIntegerField
db_index=True,
# ^-- Index for faster queries.
default=modelqueue.Status.waiting,
# ^-- Waiting state is ready to run.
)
In appname/management/commands/process_tasks.py:
import modelqueue, time
from django.core.management.base import BaseCommand
from .models import Task
class Command(BaseCommand):
def handle(self, *args, **options):
while True:
task = modelqueue.run(
Task.objects.all(),
# ^-- Queryset of models to process.
'status',
# ^-- Field name for model queue.
self.process,
# ^-- Callable to process model.
)
if task is None:
time.sleep(1)
# ^-- Bring your own parallelism/concurrency.
def process(self, task):
pass # Process task models.
In appname/admin.py:
class TaskAdmin(admin.ModelAdmin):
actions = [*modelqueue.admin_actions('status')]
# ^-- Change task status in admin.
list_filter = [
modelqueue.admin_list_filter('status'),
# ^-- Filter tasks in admin by queue state.
]
def get_changeform_initial_data(self, request):
# v-- Automatically fill in status field when adding a new task.
return {'status': int(modelqueue.Status.waiting())}