Django celery异步任务实现代码示例


Posted in Python onNovember 26, 2020

最近项目中用到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_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') #修改项目配置文件的地址
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 Queue
BROKER_URL = 'amqp://用户名:密码@ip:5672'# 指定 Broker
CELERY_RESULT_BACKEND = 'rpc://用户名:密码@ip:5672'# 指定 Backend
CELERY_TIMEZONE='Asia/Shanghai'# 指定时区,默认是 UTC
CELERY_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 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

启动flower监控

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

查看监控,注意这里的监控数据是不持久化的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python中global与nonlocal比较
Nov 21 Python
解读! Python在人工智能中的作用
Nov 14 Python
Python实现的栈(Stack)
Jan 26 Python
PyQt5主窗口动态加载Widget实例代码
Feb 07 Python
详解Python可视化神器Yellowbrick使用
Nov 11 Python
使用matplotlib绘制图例标签中带有公式的图
Dec 13 Python
Pytorch 搭建分类回归神经网络并用GPU进行加速的例子
Jan 09 Python
一文详述 Python 中的 property 语法
Sep 01 Python
Python通过类的组合模拟街道红绿灯
Sep 16 Python
python使用selenium爬虫知乎的方法示例
Oct 28 Python
使用pandas读取表格数据并进行单行数据拼接的详细教程
Mar 03 Python
python 爬取华为应用市场评论
May 29 Python
Django通过设置CORS解决跨域问题
Nov 26 #Python
Django利用elasticsearch(搜索引擎)实现搜索功能
Nov 26 #Python
python模拟点击在ios中实现的实例讲解
Nov 26 #Python
如何在 Matplotlib 中更改绘图背景的实现
Nov 26 #Python
python 实现超级玛丽游戏
Nov 25 #Python
python 制作简单的音乐播放器
Nov 25 #Python
python中绕过反爬虫的方法总结
Nov 25 #Python
You might like
php中使用接口实现工厂设计模式的代码
2012/06/17 PHP
php遍历所有文件及文件夹的方法深入解析
2013/06/08 PHP
PHP错误机制知识汇总
2016/03/24 PHP
简单实现php上传文件功能
2017/09/21 PHP
浅析PHP数据导出知识点
2018/02/17 PHP
PHP添加PNG图片背景透明水印操作类定义与用法示例
2019/03/12 PHP
jquery 插件之仿“卓越亚马逊”首页弹出菜单效果
2008/12/25 Javascript
javascript 最常用的10个自定义函数[推荐]
2009/12/26 Javascript
jQuery下实现等待指定元素加载完毕(可改成纯js版)
2013/07/11 Javascript
JavaScript中toString()方法的使用详解
2015/06/05 Javascript
JS动态添加iframe的代码
2015/09/14 Javascript
jquery图片轮播特效代码分享
2020/04/20 Javascript
实例讲解JavaScript中instanceof运算符的用法
2016/06/08 Javascript
Javascript中作用域的详细介绍
2016/10/06 Javascript
react native仿微信PopupWindow效果的实例代码
2017/08/07 Javascript
对于Javascript 执行上下文的全面了解
2017/09/05 Javascript
详解angularjs popup-table 弹出框表格指令
2017/09/20 Javascript
Vuepress 搭建带评论功能的静态博客的实现
2019/02/17 Javascript
ES7之Async/await的使用详解
2019/03/28 Javascript
详解Vue中CSS样式穿透问题
2019/09/12 Javascript
Vue中多元素过渡特效的解决方案
2020/02/05 Javascript
JavaScript组合设计模式--改进引入案例分析
2020/05/23 Javascript
Python的ORM框架中SQLAlchemy库的查询操作的教程
2015/04/25 Python
python开发之for循环操作实例详解
2015/11/12 Python
Python采用Django开发自己的博客系统
2020/09/29 Python
Python使用matplotlib模块绘制图像并设置标题与坐标轴等信息示例
2018/05/04 Python
python破解zip加密文件的方法
2018/05/31 Python
Python的Tqdm模块实现进度条配置
2021/02/24 Python
可能这些是你想要的H5软键盘兼容方案(小结)
2019/04/23 HTML / CSS
详解如何在登录过期后跳出Ifram框架
2020/09/10 HTML / CSS
意大利领先的线上奢侈品销售电商:Eleonora Bonucci
2017/10/17 全球购物
婴儿地球:Baby Earth
2018/12/25 全球购物
城市轨道专业个人求职信范文
2013/09/23 职场文书
医药个人求职信范文
2014/01/29 职场文书
学校门卫管理制度
2014/01/30 职场文书
信息与工商管理职业规划范文:为梦想而搏击
2014/09/11 职场文书