Django實(shí)現(xiàn)celery定時(shí)任務(wù)過程解析
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 自帶的用來收發(fā)消息的庫, 提供了符合 Python 語言習(xí)慣的, 使用 AMQP 協(xié)議的高級(jí)接口
Queue('transient', routing_key='transient',delivery_mode=1)
2.在settings.py里配置celery
INSTALLED_APPS = [ ...... 'django_celery_beat', 'django_celery_results', ]
3.在項(xiàng)目同名目錄下的__init__.py文件里申明celery任務(wù),記得要去檢測(cè)呀
# coding:utf-8 from __future__ import absolute_import, unicode_literals # This will make sure the app is always imported when # Django starts so that shared_task will use this app. from celery import app as celery_app __all__ = ['celery_app'] import pymysql pymysql.install_as_MySQLdb()
4.在task.py里執(zhí)行任務(wù)的函數(shù)上加@
from celery import task # 定時(shí)任務(wù) @task def push_host_by_salt_tasks(): “”“balabala”“” return '這里是定時(shí)任務(wù)'
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- django中celery的定時(shí)任務(wù)使用
- django-celery-beat搭建定時(shí)任務(wù)的實(shí)現(xiàn)
- Django初步使用Celery處理耗時(shí)任務(wù)和定時(shí)任務(wù)問題
- 關(guān)于Django使用 django-celery-beat動(dòng)態(tài)添加定時(shí)任務(wù)的方法
- Django+Celery實(shí)現(xiàn)定時(shí)任務(wù)的示例
- Django+Celery實(shí)現(xiàn)動(dòng)態(tài)配置定時(shí)任務(wù)的方法示例
- Django中使用Celery執(zhí)行定時(shí)任務(wù)問題
相關(guān)文章
Python3利用openpyxl讀寫Excel文件的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Python3利用openpyxl讀寫Excel文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Python 數(shù)據(jù)分析之逐塊讀取文本的實(shí)現(xiàn)
這篇文章主要介紹了Python 數(shù)據(jù)分析之逐塊讀取文本的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python 2與Python 3版本和編碼的對(duì)比
這篇文章主要介紹了Python 2與Python 3版本和編碼的對(duì)比,文中介紹的很詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02
python+webdriver自動(dòng)化環(huán)境搭建步驟詳解
在本篇文章里小編給大家分享了關(guān)于python+webdriver自動(dòng)化環(huán)境搭建的詳細(xì)步驟以及注意點(diǎn),需要的朋友們參考下。2019-06-06
python ImageDraw類實(shí)現(xiàn)幾何圖形的繪制與文字的繪制
這篇文章主要介紹了python ImageDraw類實(shí)現(xiàn)幾何圖形的繪制與文字的繪制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
mac 安裝python網(wǎng)絡(luò)請(qǐng)求包requests方法
今天小編就為大家分享一篇mac 安裝python網(wǎng)絡(luò)請(qǐng)求包requests方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06

