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

您的位置:首頁技術文章
文章詳情頁

Django celery異步任務實現代碼示例

瀏覽:160日期:2024-09-17 15:00:23

最近項目中用到celery很多,Django快速接入celery,這里給份教程。

準備

pip安裝celery、flower、eventlet

Django celery異步任務實現代碼示例

快速接入

1.項目目錄的__init__文件

from __future__ import absolute_import# This will make sure the app is always imported when# Django starts so that shared_task will use this app.from .celerypro import app as celery_app

2.celerypro.py文件

from __future__ import absolute_importimport osfrom celery import Celeryfrom django.conf import settings# set the default Django settings module for the ’celery’ program.os.environ.setdefault(’DJANGO_SETTINGS_MODULE’, ’voice_quality_assurance_configure.settings’) #修改項目配置文件的地址app = Celery(’voice_quality_assurance_configure’) #修改項目目錄名稱# Using a string here means the worker will not have to# pickle the object when using Windows.app.config_from_object(’voice_quality_assurance_configure.celeryconfig’) #修改celery配置文件的地址app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

3.celeryconfig.py文件,更多配置項,可以查看官方文檔。

from kombu import QueueBROKER_URL = ’amqp://用戶名:密碼@ip:5672’# 指定 BrokerCELERY_RESULT_BACKEND = ’rpc://用戶名:密碼@ip:5672’# 指定 BackendCELERY_TIMEZONE=’Asia/Shanghai’# 指定時區,默認是 UTCCELERY_TASK_SERIALIZER = ’pickle’CELERY_RESULT_SERIALIZER = ’pickle’CELERY_ACCEPT_CONTENT = [’pickle’, ’json’]CELERY_IGNORE_RESULT = True# CELERY_TIMEZONE=’UTC’CELERY_IMPORTS = ( # 指定導入的任務模塊 ’apps.mission.tasks’)CELERY_QUEUES = ( Queue(’default’, routing_key=’default’), #聲明隊列和對應路由鍵 Queue(’worker_queue’, routing_key=’worker’), #聲明隊列和對應路由鍵)CELERY_ROUTES = { ’apps.mission.tasks.createsingletask’: {’queue’: ’worker_queue’, ’routing_key’: ’worker’},}

app代碼如何使用

app下新建tasks.py文件,名字一定要是tasks。(我這里是mission app下的tasks.py)

from celery import shared_task@shared_task()def createsingletask(): print(test)

app下views調用如下:(我這里是mission app下的views.py)

from .tasks import createsingletask

createsingletask.apply_async(())

快速測試和監控

啟動多個celery worker,-A 指定項目目錄, -P 指定方式,我這里以協程方式運行, -n指定name

celery worker -A voice_quality_assurance_configure --loglevel=info -P eventlet -n worker1celery worker -A voice_quality_assurance_configure --loglevel=info -P eventlet -n worker2celery worker -A voice_quality_assurance_configure --loglevel=info -P eventlet -n worker3celery worker -A voice_quality_assurance_configure --loglevel=info -P eventlet -n worker4celery worker -A voice_quality_assurance_configure --loglevel=info -P eventlet -n worker5

啟動flower監控

celery flower --broker=amqp://用戶名:密碼@ip:5672 --broker-api=http://用戶名:密碼@ip:15672/api/

查看監控,注意這里的監控數據是不持久化的。

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

標簽: Django
相關文章:
主站蜘蛛池模板: 肇庆市| 石泉县| 札达县| 沂源县| 中宁县| 民勤县| 衡南县| 济阳县| 修武县| 汽车| 潼关县| 罗源县| 观塘区| 新乡县| 新密市| 东丽区| 新蔡县| 青铜峡市| 葫芦岛市| 通榆县| 德昌县| 万荣县| 嵊泗县| 甘德县| 泽库县| 封开县| 西乌| 南阳市| 萍乡市| 乌兰浩特市| 建宁县| 白银市| 清新县| 榆社县| 民权县| 安阳市| 凤山县| 台北县| 托克逊县| 阿尔山市| 博兴县|