Django celery异步任务实现代码示例
作者:yaominghui 时间:2021-12-10 21:38:40
最近项目中用到celery很多,Django快速接入celery,这里给份教程。
准备
pip安装celery、flower、eventlet
快速接入
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/
查看监控,注意这里的监控数据是不持久化的。
来源:https://www.cnblogs.com/-wenli/p/13723910.html
标签:Django,celery,异步,任务
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python利用matplotlib模块数据可视化绘制3D图
2023-07-25 07:48:24
![](https://img.aspxhome.com/file/2023/5/101155_0s.png)
关于Python中的if __name__ == __main__详情
2022-05-04 10:16:28
![](https://img.aspxhome.com/file/2023/2/127322_0s.png)
Python排序搜索基本算法之希尔排序实例分析
2023-03-27 14:06:07
![](https://img.aspxhome.com/file/2023/9/110369_0s.jpg)
python定时器使用示例分享
2023-04-06 15:03:57
PHP爬虫框架盘点
2023-06-10 07:10:12
![](https://img.aspxhome.com/file/2023/8/55498_0s.jpg)
pytorch中fuse_modules源码解读
2023-09-15 20:58:01
![](https://img.aspxhome.com/file/2023/7/97347_0s.png)
利用python汇总统计多张Excel
2023-12-31 14:22:11
![](https://img.aspxhome.com/file/2023/0/77690_0s.jpg)
python单线程实现多个定时器示例
2023-05-11 08:59:18
Javascript命名禁区[110407.updated]
2011-04-28 09:48:00
通过python-pptx模块操作ppt文件的方法
2022-07-20 09:41:33
整理Python 常用string函数(收藏)
2021-08-25 19:21:52
Caffe卷积神经网络视觉层Vision Layers及参数详解
2023-02-28 02:06:41
![](https://img.aspxhome.com/file/2023/5/84985_0s.png)
python数字图像处理实现图像的形变与缩放
2023-01-14 19:45:01
![](https://img.aspxhome.com/file/2023/6/131976_0s.png)
python提示No module named images的解决方法
2022-01-30 21:05:11
比较不错的函数式JavaScript编程指南教程
2023-08-25 08:24:41
Google logo “我的中国”谷歌国际少年绘画大赛小学1-3年级
2008-12-19 12:26:00
![](https://img.aspxhome.com/file/UploadPic/200812/19/j1-3_01-72s.jpg)
python递归打印某个目录的内容(实例讲解)
2021-02-13 00:54:27
asp组件上传
2010-05-27 12:16:00
如何使用Python多线程测试并发漏洞
2021-11-18 08:25:52
![](https://img.aspxhome.com/file/2023/9/66299_0s.jpg)
如何使用Pytorch完成图像分类任务详解
2023-10-05 16:37:05
![](https://img.aspxhome.com/file/2023/3/131363_0s.jpg)