国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Django實現(xiàn)celery定時任務過程解析

瀏覽:167日期:2024-10-10 11:12:55

1.首先在項目同名目錄下建一個celery.py

from __future__ import absolute_importimport osfrom celery import Celeryfrom datetime import timedeltafrom 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 settingsapp = Celery(’OpsManage’)# Using a string here means the worker will not have to# pickle the object when using Windows.# 配置celeryclass 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)# 到各個APP里自動發(fā)現(xiàn)tasks.py文件app.autodiscover_tasks()#crontab configapp.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點 # ’schedule’: crontab(minute=0, hour=0) }, },)# kombu : Celery 自帶的用來收發(fā)消息的庫, 提供了符合 Python 語言習慣的, 使用 AMQP 協(xié)議的高級接口Queue(’transient’, routing_key=’transient’,delivery_mode=1)

2.在settings.py里配置celery

INSTALLED_APPS = [ ...... ’django_celery_beat’, ’django_celery_results’,]

3.在項目同名目錄下的__init__.py文件里申明celery任務,記得要去檢測呀

# coding:utf-8from __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 pymysqlpymysql.install_as_MySQLdb()

4.在task.py里執(zhí)行任務的函數(shù)上加@

from celery import task# 定時任務@taskdef push_host_by_salt_tasks(): “”“balabala”“” return ’這里是定時任務’

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Django
相關(guān)文章:
主站蜘蛛池模板: 通道| 博爱县| 宜黄县| 绥芬河市| 泌阳县| 特克斯县| 徐州市| 康保县| 西峡县| 财经| 海口市| 栾川县| 福鼎市| 礼泉县| 焦作市| 江陵县| 新宁县| 广灵县| 新乡县| 静安区| 资中县| 远安县| 苍溪县| 司法| 武隆县| 通河县| 尼玛县| 东山县| 锡林浩特市| 灵寿县| 上犹县| 綦江县| 商洛市| 阿图什市| 高邮市| 绥江县| 于都县| 贞丰县| 普兰店市| 达拉特旗| 黎平县|