Django celery異步任務(wù)實(shí)現(xiàn)代碼示例
最近項(xiàng)目中用到celery很多,Django快速接入celery,這里給份教程。
準(zhǔn)備
pip安裝celery、flower、eventlet

快速接入
1.項(xiàng)目目錄的__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_import
import os
from celery import Celery
from 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') #修改項(xiàng)目配置文件的地址
app = Celery('voice_quality_assurance_configure') #修改項(xiàng)目目錄名稱
# 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文件,更多配置項(xiàng),可以查看官方文檔。
from kombu import Queue
BROKER_URL = 'amqp://用戶名:密碼@ip:5672'# 指定 Broker
CELERY_RESULT_BACKEND = 'rpc://用戶名:密碼@ip:5672'# 指定 Backend
CELERY_TIMEZONE='Asia/Shanghai'# 指定時(shí)區(qū),默認(rèn)是 UTC
CELERY_TASK_SERIALIZER = 'pickle'
CELERY_RESULT_SERIALIZER = 'pickle'
CELERY_ACCEPT_CONTENT = ['pickle', 'json']
CELERY_IGNORE_RESULT = True
# CELERY_TIMEZONE='UTC'
CELERY_IMPORTS = (
# 指定導(dǎo)入的任務(wù)模塊
'apps.mission.tasks'
)
CELERY_QUEUES = (
Queue('default', routing_key='default'), #聲明隊(duì)列和對(duì)應(yīng)路由鍵
Queue('worker_queue', routing_key='worker'), #聲明隊(duì)列和對(duì)應(yīng)路由鍵
)
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調(diào)用如下:(我這里是mission app下的views.py)
from .tasks import createsingletask
createsingletask.apply_async(())
快速測(cè)試和監(jiān)控
啟動(dòng)多個(gè)celery worker,-A 指定項(xiàng)目目錄, -P 指定方式,我這里以協(xié)程方式運(yùn)行, -n指定name
celery worker -A voice_quality_assurance_configure --loglevel=info -P eventlet -n worker1 celery worker -A voice_quality_assurance_configure --loglevel=info -P eventlet -n worker2 celery worker -A voice_quality_assurance_configure --loglevel=info -P eventlet -n worker3 celery worker -A voice_quality_assurance_configure --loglevel=info -P eventlet -n worker4 celery worker -A voice_quality_assurance_configure --loglevel=info -P eventlet -n worker5
啟動(dòng)flower監(jiān)控
celery flower --broker=amqp://用戶名:密碼@ip:5672 --broker-api=http://用戶名:密碼@ip:15672/api/
查看監(jiān)控,注意這里的監(jiān)控?cái)?shù)據(jù)是不持久化的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python數(shù)據(jù)可視化之基于pyecharts實(shí)現(xiàn)的地理圖表的繪制
今天給大家?guī)?lái)的是Python的相關(guān)知識(shí),文章圍繞著pyecharts繪制地理圖表展開(kāi),文中有非常詳細(xì)的代碼示例及介紹,需要的朋友可以參考下2021-06-06
Python實(shí)現(xiàn)遍歷大量表格文件并篩選出數(shù)據(jù)缺失率低的文件
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)遍歷大量表格文件并篩選出表格內(nèi)數(shù)據(jù)缺失率低的文件的功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05
Python使用自帶的ConfigParser模塊讀寫(xiě)ini配置文件
這篇文章主要介紹了Python使用自帶的ConfigParser模塊讀寫(xiě)ini配置文件的方法,ConfigParser中包含了對(duì)ini的節(jié)section的一些基本操作,使得改寫(xiě)ini時(shí)非常簡(jiǎn)便,需要的朋友可以參考下2016-06-06
python二分查找算法的遞歸實(shí)現(xiàn)方法
這篇文章主要介紹了python二分查找算法的遞歸實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Python二分查找算法的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-05-05

