使用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
相关推荐
岁月宁静5 分钟前
五、《从零手撸 Agent》 — 什么是 Agent:从“会聊天”到“会做事”,差的就是一个循环
python·agent
梯度下降者38 分钟前
Python - 函数
python·高阶函数·可调用对象·functools·嵌套作用域
starzy19903 小时前
深入理解Scala隐式参数:上下文流转
开发语言·python·scala
wuyk5554 小时前
Spider-第16讲-线程池与竞态问题
大数据·爬虫·python·elasticsearch·搜索引擎
kyle~10 小时前
FastAPI --- 将业务能力以 API 的形式提供给外部系统
python·fastapi·前后端
aiotto资源分享10 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
梯度下降者11 小时前
Python信号量,条件变量,事件学习
python·条件变量·事件·信号量·线程通信
MSTcheng.11 小时前
【AI应用实战】用 FastAPI + SSE 做一个自己的 AI 聊天网页:流式输出、多轮对话与完整源码
人工智能·python·大模型·fastapi·sse·ai聊天助手
the局外人12 小时前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python
__underline__12 小时前
冷门Python知识点006篇:Python 类体里的名字,能被"凭空造出来"
后端·python