使用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
相关推荐
小小张说故事4 分钟前
Python 的 int 为什么不会溢出?拆开 PyLongObject 和它背后那串变长数组
python
威哥爱编程37 分钟前
Python 智能体开发实战 03:聊十轮就“失忆”,账单还翻倍?管好上下文,Token 立省一半
python·agent
情 九38 分钟前
MCP 开发环境搭建:Mac 与 Windows 跑通 Python 服务和 stdio 客户端
windows·python·macos·mcp
SEO_juper1 小时前
Bash 交叉验证 robots.txt 和 sitemap:两个文件自相矛盾,页面永远收录不了
开发语言·python·node.js·json·bash
日日大王学编程1 小时前
LangGraph 基本用法详解:从入门到实战
python·语言模型·langchain
威哥爱编程1 小时前
Python 智能体开发实战 04:模型答得挺漂亮,程序却读不懂?让回答直接变成能入库的 JSON
python·agent
全栈陈序员1 小时前
【Python】基础语法入门(三十)—— Web 开发入门:用 Flask 搭建第一个网站
前端·python·flask
全栈陈序员1 小时前
【Python】基础语法入门(三十三)—— 日志系统详解:用 logging 记录程序运行
java·开发语言·python
晗夜未央1 小时前
跨仓库 Skill 换台电脑就失效?用 Git remote 校验项目
人工智能·git·python
Zack_Liu1 小时前
code样例
pytorch·python·深度学习