当前位置:网站首页>Celery 异步调用方法改动记录
Celery 异步调用方法改动记录
2022-06-10 13:39:00 【tnan2522】
Celery 是一个任务队列, 可以异步处理
在4.0 版本后python使用celery 会出现种种错误, 这时候需要安装 eventlet模块
pip install eventlet
默认是
celery -A task worker --loglevel=info
启动celery
4.0后启动celery:
celery -A task worker --loglevel=info -P eventlet
在work 装饰器上也要改动
默认是:
import time
from celery import Celery
app = Celery(
broker='amqp://[email protected]//', # 消息队列的url
backend='redis://localhost:6379/9' # 将调用的结果存储到Redis中
)
@app.task
def add(x, y):
print("celery 接收到的数据是: ", x, y)
print("x+y=", x+y)
# time.sleep(100)
return x + y
改为:
import time
from celery import Celery
app = Celery(
broker='amqp://[email protected]//', # 消息队列的url
backend='redis://localhost:6379/9' # 将调用的结果存储到Redis中
)
@app.task(name="aaabbb")
def add(x, y):
print("celery 接收到的数据是: ", x, y)
print("x+y=", x+y)
# time.sleep(100)
return x + y
这样做是因为如果不给任务队列设置名字的话,队列会根据当前的模块名和方法名当做队列的名字, 如aaabbb
在其他模块中导入使用是, 如
from celery_aabb.task import add
for i in range(50):
add.delay(i, i + 10)
add.delay 给任务队列发送消息的时候发送给的队列是 celery_aabb.task.add, 这样就找不到对应的任务队列处理了,就会出现异常
如果设置了队列的名字,那么, 在导入add 使用的时候会使用celery 装饰命名的名字当做发送的任务队列的名字,这样就找到了任务处理队列
在远程调用的时候,
from celery import Celery
app = Celery(
broker='amqp://[email protected]//', # 消息队列的url
)
app.send_task("aabbcc", [10, 20])
只需要写上命名任务队列就可以找到 celery 中启动的任务队列
保存执行结果:
backend='redis://localhost:6379/8'
将调用的结果存储到Redis中, 储存格式 是 json 序列化的字符串
在add 处理函数中会将return 返回值当做要保储的值储存到redis 中, 如果没有返回值,celery 也是会储存数据, 不过储存字符串中的restful为null
celery 发送邮件
在task 工作模块中导入其他模块,因为是在命令行模式启动的, 所以它只能够找当前目录下的文件和文件夹, 不能找到上一层的文件和文件夹,
需要将发送邮件的模块放到task 工作模块的目录下
边栏推荐
- [Huang ah code] teacher, I want to choose software development related majors after the college entrance examination. Which direction do you think is better? How to fill in the college entrance examin
- SnackBar usage details
- High performance practical Alibaba sentinel notes, in-depth restoration of Alibaba micro service high concurrency scheme
- Libre circulation des ressources dans le cloud et localement
- 什么是基因的p值?
- 解决VMWareStation安装 tools 时 D:\setup.exe 找不到的问题
- [yellow code] SVN version control tutorial
- 解决VMware Workstation安装VMware Tools显示灰色的办法
- [笔记]Windows安全之《三》Shellcode 补充之 Get-InjectedThread脚本搭建环境及其使用
- buuctf [GlassFish]任意文件读取
猜你喜欢

【C语言】指针函数与函数指针、数组函数

Copying and deleting files

32、树莓派的简单测试串口通信和超声波模块测距

Docker部署一个Redis集群

CL210OpenStack操作的故障排除--常见核心问题的故障排除

Solve the problem that win10 virtual machine and host cannot paste and copy each other

win10虚拟机下载安装流程

What happened when the legendary login prompt failed to connect to the server? How to solve it?

CentOS Linux 已死!Oracle Linux 可能是它的更好替代品

【无标题】音频蓝牙语音芯片,WT2605C-32N实时录音上传技术方案介绍
随机推荐
lua 表操作
In depth analysis of "circle group" relationship system design | series of articles on "circle group" technology
多云管理平台cmp是什么意思?谁能清楚解释一下
[FAQ] summary of common problems and solutions during the use of rest API interface of sports health service
How to locate the hot problem of the game
Smart campus security channel and video monitoring solution
解决跨海高并发崩溃难题?so easy
【Golang】创建有配置参数的结构体时,可选参数应该怎么传?
32. Simple test of raspberry pie serial port communication and ultrasonic module ranging
buuctf [Jupyter]notebook-rce
大四应届毕业生,想自学软件测试,如何应对面试?
odoo 权限管理(访问权限及记录规则)结合实用,升级角色管理
buuctf [PHP]inclusion
What does the multi cloud management platform CMP mean? Who can explain it clearly
im即时通讯开发:进程被杀底层原理、APP应对被杀技巧
3. web page development tool vs Code
Solve the problem of cross sea high concurrent crash? so easy
Win10 virtual machine download and installation process
apache atlas 快速入门
H. 265 introduction to coding principles