django-q轻量级定时任务制定

django-q ,celery,apschedule都可以作为python的选型,但是django-q更轻量级,可以定制想要的任务,通过消息中间件,来实现不太高并发的实现
官网介绍地址
django-q官网地址

本次测试的是python3.12版本
首先需要安装django-q,具体安装方式pip install django-q
国内的话可以选择清华或者豆瓣源进行安装
最后在settings.py文件中INSTALLED_APPS添加

INSTALLED_APPS = [
	#放在最后面添加即可
    'django_q',

]

然后配置队列消息中间件
相关消息中间件配置可以参照官网
中间件选型
为了测试方便选择了orm配置
settings.py添加如下内容

Q_CLUSTER = {
    'name': 'DjangORM',
    'workers': 1,
    'timeout': 90,
    'retry': 120,
    'queue_limit': 50,
    'bulk': 10,
    'orm': 'default'
}

为了方便views.py视图,自己创建了一个app01项目
核心代码如下

from django.shortcuts import render,HttpResponse
from django.http import JsonResponse,request

# Create your views here.

import math


def demo_task(number):
    print(number,type(number))
    return math.sqrt(number)


from django_q.tasks import async_task, Task


def task_finish(task: Task):
    print(f'任务 {task.name}(ID:{task.id})完成!')
# hook参数是执行完成回调函数,可以通过admin后台查看结果
def create_task(request):
    task_id=async_task(
        demo_task, 10,
        task_name='测试任务1',
        hook=task_finish,
    )
    return JsonResponse({'task_id':task_id,'result':'成功'})


from django_q.tasks import schedule,Schedule

def create_cron1(request):
    schedule(
        'app01.views.demo_task',1000,
        schedule_type=Schedule.MINUTES,
        minutes=1,
        task_name='定时任务1',
    )
    return JsonResponse({'result':'定时任务1创建'})

配置相关的urls.py路由前端能识别到
在这里插入图片描述
执行每个命令分别新开一个窗口执行,前三步必须顺序执行,后两步骤可部分先后顺序执行

python manage.py makemigrations
python manage.py migrate
python manage.py runserver
python manage.py createsuperuser
python manang.py qcluster

打开浏览器执行
在这里插入图片描述
可以看到定时任务已经创建
在这里插入图片描述
为什么是两个定时任务因为有一个是我之前创建
在这里插入图片描述
可以看到里面的参数,我们可以定制修改

定时任务执行结果
在这里插入图片描述

Django-Q的定时任务类型:

一次性

按x分钟执行一次

每小时一次

每天

每周

每月

每季度

每年

Cron表达式

注意,即使是Cron表达式,定时任务执行的最短间隔也是1分钟缘由

The current design has a heartbeat of 30 seconds, which means the schedule table can't have schedules below that. Most of this is explained in the architecture docs. Because of the way the internal loop is set up, a resolution under a dozen seconds or so, quickly becomes unreliable.

I always imagined tasks that need accuracy measured in seconds, would use a delayed tasks strategy where a few seconds delay is either added through the broker or inside the task itself.

The problem with all this, is that a task is currently always added to the back of the queue. So even with a 1 second resolution on the schedule, the task still has to wait it's execution time. Which can of course vary wildly depending on the broker type, worker capacity and current workload.

当然如果触发了定时任务也可以触发信号
signal官网文档

具体完整版本可以参考附件下载

相关推荐

  1. Python 轻量级定时任务调度工具

    2024-03-15 05:50:02       59 阅读
  2. django 定时任务的创建

    2024-03-15 05:50:02       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 05:50:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 05:50:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 05:50:02       82 阅读
  4. Python语言-面向对象

    2024-03-15 05:50:02       91 阅读

热门阅读

  1. T2080 AR8031 RGMII to 1000Base-X(fiber)

    2024-03-15 05:50:02       43 阅读
  2. BERT:深度学习领域中的语言理解利器

    2024-03-15 05:50:02       46 阅读
  3. exec 和 xargs 命令的用法区别,优缺点

    2024-03-15 05:50:02       46 阅读
  4. git基础命令(二)

    2024-03-15 05:50:02       58 阅读
  5. 使用docker搭建mongodb

    2024-03-15 05:50:02       35 阅读
  6. Linux系统之部署react-tetris俄罗斯方块小游戏

    2024-03-15 05:50:02       36 阅读