金狮镖局 Design By www.egabc.com
继上一篇中间表的数据是动态的,图表展示的数据才比较准确。这里用到一个新的模块Djcelery,安装配置步骤如下:
1.安装
redis==2.10.6
celery==3.1.23
django-celery==3.1.17
flower==0.9.2
supervisor==3.3.4
flower用于监控定时任务,supervisor管理进程,可选
2.配置
settings.py中添加以下几行:
#最顶头加上 from __future__ import absolute_import # celery settings import djcelery djcelery.setup_loader() BROKER_URL = 'redis://localhost:6379' # BROKER_URL = 'redis://:密码@主机地址:端口号/数据库号' CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler' # 定时任务 CELERY_RESULT_BACKEND = 'djcelery.backends.database:DatabaseBackend' CELERY_RESULT_BACKEND = 'redis://localhost:6379' CELERY_ACCEPT_CONTENT = ['application/json'] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' CELERYD_MAX_TASKS_PER_CHILD = 40 CELERY_TIMEZONE = 'Asia/Shanghai' INSTALLED_APPS = [ 'djcelery',# 添加djcelery ]
3.注册定时任务的几个表
from __future__ import absolute_import, unicode_literals from djcelery.models import ( TaskState, WorkerState, PeriodicTask, IntervalSchedule, CrontabSchedule, ) from xadmin.sites import site site.register(IntervalSchedule) # 存储循环任务设置的时间 site.register(CrontabSchedule) # 存储定时任务设置的时间 site.register(PeriodicTask) # 存储任务 site.register(TaskState) # 存储任务执行状态 site.register(WorkerState) # 存储执行任务的worker
4.主应用下添加celery.py
__init__.py修改如下:
# __init__.py from __future__ import absolute_import from .celery import app as celery_app # celery.py from __future__ import absolute_import import os from celery import Celery, platforms from django.conf import settings # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hermes.settings') # hermes主应用名 app = Celery('hermes') platforms.C_FORCE_ROOT = True app.config_from_object('django.conf:settings') app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) @app.task(bind=True) def debug_task(self): print('Request: {0!r}'.format(self.request))
5.添加任务 应用下添加tasks.py
from __future__ import absolute_import from celery import task import time from .channels import Cache_data_to_redis # 更新指定日期数据到sms_organizationcount @task def readAndWrite(begin,end): begin = str(begin)[:4] + '-' + str(begin)[4:6] + '-' + str(begin)[6:8] end = str(end)[:4] + '-' + str(end)[4:6] + '-' + str(end)[6:8] i = 0 begin_time = time.time() read = Cache_data_to_redis().connection Rcursor = read.cursor() query = "SELECT id from sms_organizationcount WHERE alia_date_time between '" query += begin query += "' and '" query += end query += "'" readSql = "SELECT alia_month_time, alia_date_time, count(*) as total_nums, count(t.`status`=2 or null) as error_nums, name FROM (select *, DATE_FORMAT(req_time,'%Y-%m') as alia_month_time, DATE_FORMAT(req_time,'%Y-%m-%d') as alia_date_time, LEFT(body,LOCATE('】',body)) as name from sms_smslog where LOCATE('】',body) >0 and LEFT(body,1)='【' and DATE_FORMAT(req_time,'%Y-%m-%d') between '" readSql += begin readSql += "' and '" readSql += end readSql += "')" readSql += " as t GROUP BY alia_date_time , name;" Rcursor.execute(readSql) readResult = Rcursor.fetchall() Rcursor.execute(query) query_result = Rcursor.fetchall() deleteSql = "delete from sms_organizationcount where alia_date_time between '%s' and '%s'" % (begin,end) if query_result: delete_record = Cache_data_to_redis().connection Dcursor = delete_record.cursor() Dcursor.execute(deleteSql) delete_record.commit() delete_record.close() for value in readResult: write = Cache_data_to_redis().connection Wcursor = write.cursor() writeSql = "INSERT into sms_organizationcount (alia_month_time, alia_date_time, total_nums, error_nums, `name`) " " VALUES ('%s', '%s', '%s', '%s', '%s' )" % (value['alia_month_time'], value['alia_date_time'], value['total_nums'], value['error_nums'], value['name']) try: Wcursor.execute(writeSql) i += 1 write.commit() except: write.rollback() write.close() read.close() end_time = time.time() pass_time = end_time - begin_time return i, pass_time
6.最终效果如下图:
7.终端启动celery命令:
# 查看注册的task celery -A hermes inspect registered # 启动 python manage.py celery -A django_celery_demo worker -B # django_celery_demo为celery和setting所在文件夹名 #celery_beat起不来 # 动态的输出启动进程时的输出 supervisorctl tail programname stdout # flower监控celery python manage.py celery flower ip:5555
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无django+xadmin+djcelery实现后台管理定时任务的评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年11月19日
2024年11月19日
- 好薇2024《兵哥哥》1:124K黄金母盘[WAV+CUE]
- 胡歌.2006-珍惜(EP)【步升大风】【FLAC分轨】
- 洪荣宏.2014-拼乎自己看【华特】【WAV+CUE】
- 伊能静.1999-从脆弱到勇敢1987-1996精选2CD【华纳】【WAV+CUE】
- 刘亮鹭《汽车DJ玩主》[WAV+CUE][1.1G]
- 张杰《最接近天堂的地方》天娱传媒[WAV+CUE][1.1G]
- 群星《2022年度发烧天碟》无损黑胶碟 2CD[WAV+CUE][1.4G]
- 罗文1983-罗文甄妮-射雕英雄传(纯银AMCD)[WAV+CUE]
- 群星《亚洲故事香港纯弦》雨果UPMAGCD2024[低速原抓WAV+CUE]
- 群星《经典咏流传》限量1:1母盘直刻[低速原抓WAV+CUE]
- 庾澄庆1993《老实情歌》福茂唱片[WAV+CUE][1G]
- 许巍《在别处》美卡首版[WAV+CUE][1G]
- 林子祥《单手拍掌》华纳香港版[WAV+CUE][1G]
- 郑秀文.1997-我们的主题曲【华纳】【WAV+CUE】
- 群星.2001-生命因爱动听电影原创音乐AVCD【MEDIA】【WAV+CUE】