使用django实现windows任务调度管理

在 Django 中实现 Windows 任务调度管理,你可以使用几种不同的方法。最常见的方法是使用 Django 自带的 celery 或者 django-background-tasks 库,或者使用 Windows 自带的任务计划程序。下面我会分别介绍这几种方法:

方法 1:使用 Celery

Celery 是一个强大的异步任务队列/作业队列,基于分布式消息传递。它支持多种消息中间件,包括 Redis, RabbitMQ 等。

步骤:

安装 Celery

css 复制代码
pip install celery

配置 Celery

在 Django 项目中创建一个 celery.py 文件,例如在 your_project 目录下:

css 复制代码
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
 
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
 
app = Celery('your_project')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

配置 init.py

在 your_app 目录下添加 init.py 文件,以便 Django 自动发现任务:

css 复制代码
from __future__ import absolute_import, unicode_literals
 
from .celery import app as celery_app
 
__all__ = ('celery_app',)

定义任务

在 your_app 的 tasks.py 文件中定义任务:

css 复制代码
from celery import shared_task
 
@shared_task
def my_scheduled_task():
    # 你的任务代码
    print("执行定时任务")

配置 Celery 定时任务

settings.py 中配置 Celery Beat:

css 复制代码
CELERY_BEAT_SCHEDULE = {
    'my-scheduled-task': {
        'task': 'your_app.tasks.my_scheduled_task',
        'schedule': 30.0,  # 每30秒执行一次
    },
}

运行 Celery Worker 和 Beat

css 复制代码
celery -A your_project worker --loglevel=info
celery -A your_project beat --loglevel=info

方法 2:使用 django-background-tasks

django-background-tasks 是一个简单的后台任务框架,不需要额外的消息代理。

步骤:

安装 django-background-tasks

css 复制代码
pip install django-background-tasks

配置

settings.py 中添加 'background_task' 到 INSTALLED_APPS。

css 复制代码
INSTALLED_APPS = [
    ...
    'background_task',
    ...
]

定义任务

在 your_app 的 tasks.py 中定义任务:

css 复制代码
from background_task import background
 
@background(schedule=30)  # 每30秒执行一次
def my_scheduled_task():
    # 你的任务代码
    print("执行定时任务")

运行后台任务守护进程

css 复制代码
python manage.py run_background_tasks --settings=your_project.settings --loglevel=info --traceback --stdout --pidfile= --rm-pidfile --logfile= --rm-logfile --daemonize --max-workers=1 --max-tasks-per-child=1000 --timeout=300 --cleanup-expired=True --cleanup-frequency=600 --cleanup-grace=300 --cleanup-limit=10000 --cleanup-keep=10000 --cleanup-expire=1800 --cleanup-expire-grace=3600 --cleanup-expire-keep=1800 --cleanup-expire-limit=18000 --cleanup-expire-grace-keep=36000 --cleanup-expire-grace-limit=36000 --cleanup-expire-grace-keep=36000 --cleanup-expire-grace-limit=36000 --cleanup-expire-grace-keep=360
相关推荐
程序设计实验室2 小时前
当人人都能用 AI 写代码时,我为什么选择重回 Django?
django·djangostarter
zone77392 小时前
001:简单 RAG 入门
后端·python·面试
F_Quant2 小时前
🚀 Python打包踩坑指南:彻底解决 Nuitka --onefile 配置文件丢失与重启报错问题
python·操作系统
允许部分打工人先富起来3 小时前
在node项目中执行python脚本
前端·python·node.js
IVEN_4 小时前
Python OpenCV: RGB三色识别的最佳工程实践
python·opencv
haosend4 小时前
AI时代,传统网络运维人员的转型指南
python·数据网络·网络自动化
曲幽5 小时前
不止于JWT:用FastAPI的Depends实现细粒度权限控制
python·fastapi·web·jwt·rbac·permission·depends·abac
IVEN_1 天前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang1 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮1 天前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python