這篇文章主要講解了Django實(shí)現(xiàn)celery定時(shí)任務(wù)的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
1.首先在項(xiàng)目同名目錄下建一個(gè)celery.py
from __future__ import absolute_import import os from celery import Celery from datetime import timedelta from kombu import Queue # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'OpsManage.settings') from django.conf import settings app = Celery('OpsManage') # Using a string here means the worker will not have to # pickle the object when using Windows. # 配置celery class Config: BROKER_URL = 'amqp://guest:guest@localhost:5672//' CELERY_RESULT_BACKEND = 'redis://localhost:6379' CELERY_ACCEPT_CONTENT = ['application/json'] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' CELERY_TASK_RESULT_EXPIRES = 60 * 60 CELERY_TIMEZONE = 'Asia/Shanghai' CELERY_ENABLE_UTC = True CELERY_ANNOTATIONS = {'*': {'rate_limit': '500/s'}} CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler' app.config_from_object(Config) # 到各個(gè)APP里自動(dòng)發(fā)現(xiàn)tasks.py文件 app.autodiscover_tasks() #crontab config app.conf.update( CELERYBEAT_SCHEDULE = { # 每隔30s執(zhí)行一次函數(shù) 'every-30-min-add': { 'task': 'apps.tasks.celery_assets.push_host_by_salt_tasks', 'schedule': timedelta(seconds=30) # # 每天凌晨12點(diǎn) # 'schedule': crontab(minute=0, hour=0) }, }, ) # kombu : Celery 自帶的用來(lái)收發(fā)消息的庫(kù), 提供了符合 Python 語(yǔ)言習(xí)慣的, 使用 AMQP 協(xié)議的高級(jí)接口 Queue('transient', routing_key='transient',delivery_mode=1)
網(wǎng)頁(yè)題目:Django實(shí)現(xiàn)celery定時(shí)任務(wù)的方法-創(chuàng)新互聯(lián)
瀏覽路徑:http://chinadenli.net/article30/dsgcso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、App設(shè)計(jì)、企業(yè)建站、網(wǎng)站營(yíng)銷、微信小程序、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容