跳转到主要内容

基于Django模型的任务队列。

项目描述

ModelQueue 是一个基于Django模型的Apache2许可的任务队列。

例如,在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.
    )

在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.

在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())}

ModelQueue 是一个危险的项目。它将一个坏主意变得简单而有效。你可能会后悔将数据库用作任务队列,但今天不会是那样!

客户评价

“我设计关系数据库系统不是为了这个。” ~ Edgar Codd

“至少你是在使用事务。” ~ Jim Gray

“你成功地忽略了队列理论中的大多数重要内容。” ~ Agner Erlang

你的公司或网站是否使用 ModelQueue?给我们发消息并告诉我们。

功能

  • 纯Python

  • 支持Django的管理界面

  • 任务可以重试、中止和取消

  • 支持每个任务多次尝试

  • 使用线程、多进程或asyncio自行提供并行性

  • 性能很重要(在模型中添加单个64位字段)

  • 完全文档化

  • 100%测试覆盖率

  • 多年的生产环境压力测试

  • 在Python 3.10上开发

  • 与所有Django版本兼容

  • 在CPython 3.6、3.7、3.8、3.9、3.10上测试

  • 在Linux、Mac OS X和Windows上测试

https://github.com/grantjenks/django-modelqueue/workflows/integration/badge.svg https://github.com/grantjenks/django-modelqueue/workflows/release/badge.svg

快速入门

使用 ModelQueue 的安装非常简单,只需使用 pip

$ python -m pip install modelqueue

您可以使用 Python 内置的帮助功能在解释器中访问文档

>>> import modelqueue
>>> help(modelqueue)

用户指南

对于那些想要更多详细信息的人来说,本部分文档介绍了介绍、基准、开发和 API

参考和索引

ModelQueue 许可证

版权所有 2022 Grant Jenks

在 Apache 许可证 2.0 版(“许可证”)下授权;除非适用法律要求或书面同意,否则不得使用此文件,除非遵守许可证。您可以在以下位置获取许可证副本:

https://apache.ac.cn/licenses/LICENSE-2.0

除非适用法律要求或书面同意,否则根据许可证分发的软件按“原样”基础分发,不提供任何明示或暗示的保证或条件。有关许可证中规定的具体语言和限制,请参阅许可证。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分发

modelqueue-2.2.1.tar.gz(8.5 kB 查看哈希值

上传时间

构建分发

modelqueue-2.2.1-py3-none-any.whl(8.8 kB 查看哈希值

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面