使用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
相关推荐
爱昏羔6 小时前
下篇:从检索到交互 — 物流RAG问答系统与WebUI全实现
python·langchain·agent
_Jimmy_6 小时前
Agent引用数据库知识过时的增量同步方案
人工智能·python·langchain
min(a,b)8 小时前
学习第 4 天:面向对象与异常处理
python·学习·学习方法
yangshicong9 小时前
第19章:AI安全防护与AI安全
人工智能·python·安全·prompt·ai编程
果汁华9 小时前
Function Calling 与 Python 实战完整指南
开发语言·网络·python
c_lb728810 小时前
2026年不同基础做量化,先找AI能参与的位置
人工智能·python
muddjsv10 小时前
SQLite数据库文件机制详解:journal、wal、shm文件作用与备份避坑
数据库·sqlite
chenment11 小时前
ComfyUI 自定义节点开发:从零扩展你的图像生成工作流
python·stable diffusion
IT空门:门主12 小时前
Python 基础语法学习路线图
网络·python·学习
互联网中的一颗神经元12 小时前
小白python入门 - 25. SQL 与表设计入门
数据库·python·sql