settings.py

 
# Broker配置,使用Redis作为消息中间件
BROKER_URL = 'redis://127.0.0.1:6379/0' # BACKEND配置,这里使用redis
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/0' # 结果序列化方案
CELERY_RESULT_SERIALIZER = 'json' # 任务结果过期时间,秒
CELERY_TASK_RESULT_EXPIRES = 60 * 60 * 24 # 时区配置
CELERY_TIMEZONE = 'Asia/Shanghai' # 指定导入的任务模块,可以指定多个
CELERY_IMPORTS = (
'scriptOperate.tasks'
) CELERYBEAT_SCHEDULE = {
'ali_script': {
# 任务路径
'task': 'scriptOperate.tasks.get_ali_script_status',
# 每日七点执行
# 'schedule': crontab(hour=7, minute=0),
'schedule': timedelta(seconds=20)
# 'schedule':5
},
'log_file': {
# 任务路径
'task': 'scriptOperate.tasks.produce_log',
# 每日零点执行
'schedule': crontab(hour=0, minute=0), # 通过crontab进行定时
},
'weather_file':{
'task': 'scriptOperate.tasks.get_weather_status',
'schedule': crontab(hour=7, minute=0)
# 'schedule': timedelta(seconds=20)
},
'power_file':{
'task': 'scriptOperate.tasks.get_power_status',
'schedule': crontab(hour=5, minute=30),
# 'schedule': timedelta(seconds=20)
},
'overview':{
'task': 'scriptOperate.tasks.get_overview_status',
'schedule': crontab(hour=7, minute=10),
# 'schedule': timedelta(seconds=30)
}
}

settings.py同级目录下创建celery.py

import os
import django
from celery import Celery
from django.conf import settings
from celery.schedules import crontab
# 设置系统环境变量,安装django,必须设置,否则在启动celery时会报错
# celery_study 是当前项目名
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'code_work2.settings')
# django.setup() celery_app = Celery('scriptOperate')
celery_app.config_from_object('django.conf:settings')
celery_app.autodiscover_tasks(settings.INSTALLED_APPS)

在app下创建tasks.py

import logging
import json
import datetime
import time
import os.path
import re
import pysftp
from celery import shared_task
from log.log_files import Logger
from code_work2.celery import celery_app
from scriptOperate.task import ali_monitor, weather_monitor, power_monitor, over_view_monitor # log = Logger('../log/files/data.log', level='debug')
# logger = log.logger # @shared_task
@celery_app.task
def get_ali_script_status():
ali_monitor.get_ali_status() @celery_app.task
def get_weather_status():
weather_monitor.get_weather_status() @celery_app.task
def get_power_status():
power_monitor.get_power_status() @celery_app.task
def get_overview_status():
over_view_monitor.get_overview_status() @shared_task
def produce_log():
Logger(logfile, level='debug')

启动任务

celery -A code_work2 beat -l info   启动beat
celery -A code_work2 worker -l info -P eventlet 启动celery

django通过celery定时任务的更多相关文章

  1. Django与Celery配合实现定时任务

    一.前言 Celery是一个基于python开发的分布式任务队列,而做python WEB开发最为流行的框架莫属Django,但是Django的请求处理过程都是同步的无法实现异步任务,若要实现异步任务 ...

  2. Django Celery定时任务和时间设置

    1.Celery加入定时任务 Celery除了可以异步执行任务之外,还可以定时执行任务.在实例代码的基础上写个测试方法: #coding:utf- from celery.task.schedules ...

  3. Bamboo Django Celery定时任务和时间设置

    1.Celery加入定时任务 Celery除了可以异步执行任务之外,还可以定时执行任务.在实例代码的基础上写个测试方法: 1 #coding:utf-8 2 from celery.task.sche ...

  4. Django中Celery的实现介绍(一)

    Django中Celery的实现 Celery官网http://www.celeryproject.org/ 学习资料:http://docs.jinkan.org/docs/celery/ Cele ...

  5. Django使用Celery异步任务队列

    1  Celery简介 Celery是异步任务队列,可以独立于主进程运行,在主进程退出后,也不影响队列中的任务执行. 任务执行异常退出,重新启动后,会继续执行队列中的其他任务,同时可以缓存停止期间接收 ...

  6. celery 定时任务时间篇

    1.Celery加入定时任务 Celery除了可以异步执行任务之外,还可以定时执行任务.在实例代码的基础上写个测试方法: 1 #coding:utf-8 2 from celery.task.sche ...

  7. django和celery结合应用

    django+celery项目结构 - project_name - app01 - __init__.py - admin.py - views.py - modes.py - tasks.py # ...

  8. Python中Celery 的基本用法以及Django 结合 Celery 的使用和实时监控进程

    celery是什么 1 celery是一个简单,灵活且可靠的,处理大量消息的分布式系统 2 专注于实时处理的异步任务队列 3 同时也支持任务调度 执行流程 Celery 基本使用 tasks.py i ...

  9. 将celery定时任务设置为根据本地时区触发

    默认celery的时区为UTC,如果要在django项目中将celery定时任务配置为根据本地时区触发,则需要修改 在setttings.py 添加以下任意一行: # celery 相关配置 CELE ...

  10. django中设置定时任务

    django中设置定时任务 在django中设置定时任务我们可以借用django-crontab这个第三包来实现 django-crontab只能在linux系统下使用 安装: pip install ...

随机推荐

  1. Delta Lake在Soul的应用实践

    简介: 传统离线数仓模式下,日志入库前首要阶段便是ETL,我们面临如下问题:天级ETL任务耗时久,影响下游依赖的产出时间:凌晨占用资源庞大,任务高峰期抢占大量集群资源:ETL任务稳定性不佳且出错需凌晨 ...

  2. iLogtail 与Filebeat 性能对比

    ​简介:前段时间, iLogtail 阿里千万实例可观测采集器开源,其中介绍了iLogtail采集性能可以达到单核100MB/s,相比开源采集Agent有5-10倍性能优势.很多小伙伴好奇iLogta ...

  3. 快速上手 Serverless | 入门第一课

    简介: 本文从云计算抛砖引玉,详解 Serverless 的典型应用场景和一些产品介绍. 一. 从云计算到 Serverless 自世界上第一台通用计算机 ENIAC (图左)诞生以来,计算机科学与技 ...

  4. [FAQ][Hardhat] Error HH501: Couldn't download compiler version 0.8.0. Please check your connection.

    当使用 npx hardhat compile 命令编译智能合约时,会先下载你在 hardhat.config.js 配置中对应版本的 solidity 编译器. 当网络不可达时,就会提示无法下载的错 ...

  5. 2019-9-2-C#-设计模式-责任链

    title author date CreateTime categories C# 设计模式 责任链 lindexi 2019-09-02 12:57:37 +0800 2018-2-13 17:2 ...

  6. k3s入门与实战---适配边缘计算场景的轻量级的k8s(一)

    一.k3s介绍 1.1 什么是k3s? k3s 是经过 CNCF 认证的由 Rancher 公司开发维护的一个轻量级的 Kubernetes 发行版,内核机制还是和 k8s 一样,但是剔除了很多外部依 ...

  7. .Net 8.0 下的新RPC,IceRPC之试试的新玩法"打洞"

    作者引言 很高兴啊,我们来到了IceRPC之试试的新玩法"打洞",让防火墙哭去吧 试试RPCs的新玩法"打洞" 比较典型的玩法:RPC数据流从客户端流向服务端, ...

  8. Windows server 2012 R2开机进入cmd,关闭后黑屏

    出现此问题,一般只有两种情况,操作系统装置前和操作系统装置后出现: 第一种: 装置操作系统的时候没有选择"Windows Server 2012 R2 Strandard( 带有GUI的服务 ...

  9. 瑞亚时间管理大师,基于 .NET 6 和 Angular 构建的在线任务管理协作平台

    瑞亚时间管理大师 瑞亚时间管理大师, 是一个在线的任务管理.项目管理. 团队协作平台.瑞亚 拥有现代化的页面风格,高效.简便,同时适合个人和团队使用. 瑞亚对个人免费,提供了无限制的任务,列表,和空间 ...

  10. ༺$Musique Collection 1$༻

    ~~头图~~ 自取捏 <\(Landslide\)>-\(Oh\,Wonder\) I know it hurts sometimes but You'll get over it You ...