您好,登錄后才能下訂單哦!
這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)怎么在django中使用xadmin和djcelery實現(xiàn)后臺管理定時任務,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1.安裝
redis==2.10.6
celery==3.1.23
django-celery==3.1.17
flower==0.9.2
supervisor==3.3.4
flower用于監(jiān)控定時任務,supervisor管理進程,可選
2.配置
settings.py中添加以下幾行:
#最頂頭加上 from __future__ import absolute_import # celery settings import djcelery djcelery.setup_loader() BROKER_URL = 'redis://localhost:6379' # BROKER_URL = 'redis://:密碼@主機地址:端口號/數(shù)據(jù)庫號' 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) # 存儲循環(huán)任務設(shè)置的時間 site.register(CrontabSchedule) # 存儲定時任務設(shè)置的時間 site.register(PeriodicTask) # 存儲任務 site.register(TaskState) # 存儲任務執(zhí)行狀態(tài) site.register(WorkerState) # 存儲執(zhí)行任務的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 # 更新指定日期數(shù)據(jù)到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起不來 # 動態(tài)的輸出啟動進程時的輸出 supervisorctl tail programname stdout # flower監(jiān)控celery python manage.py celery flower ip:5555
上述就是小編為大家分享的怎么在django中使用xadmin和djcelery實現(xiàn)后臺管理定時任務了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。