当前位置:网站首页>Celery异步发送短信
Celery异步发送短信
2022-07-23 05:38:00 【ZXY_lucky】
1.Celery
安装celery
celery,分布式异步任务队列 pip install celery==4.4.7
eventlet,并发网络库 gevent 协程库pip install eventlet==0.26.1
Celery是一个基于python的分布式异步任务队列,服务器提交任务,celery处理,服务器给客户端发送已处理响应,不影响后面的程序运行
- 异步任务,比如发送短信,消息推送、音频处理
- 做定时任务
- 可以使用celery实现简单分布式爬虫系统
1.1 Celery分析

- Task --任务
- Borker --消息中间件,来接收任务和发送任务信息,保存任务队列
- Worker–后台执行任务,监视队列中是否有任务,有就立刻执行
- Backend–保存任务执行结果
2.异步发送短信
2.1 配置celery
在与项目名下的文件创建celery.py文件
from celery import Celery
from django.conf import settings
import os
# 配置环境变量
os.environ.setdefault("DJANGO_SETTINGS_MODULE",'p6.settings')
# 实例化celery对象
app = Celery("cc")
# 通过配置文件 进行配置
app.config_from_object("p6.config")
# 让app自动发现任务函数
app.autodiscover_tasks(settings.INSTALLED_APPS)
在该文件下创建config.py文件存储celery信息
broker_url = "redis://:[email protected]:6379/5"
result_backend = "redis://:[email protected]:6379/6"
2.2 任务
在子应用文件下创建tasks.py
from p6.celery import app
from ronglian_sms_sdk import SmsSDK
import json
accId = "8aaf07088185853e01818a75a87a016f"
accToken = "6b755e025dc2436a8e3b7b82ef9ac20c"
appId = "8aaf07088185853e01818a75a9a00176"
# 定义发送短信的函数
# 定义任务函数
@app.task
def send_message(sms_code,mobile,expire=5):
""" :param sms_code: 要发送的验证码 :param mobile: 发送的手机号 :param expire: 过期时间 :return: """
# 实例化sdk对象
sdk = SmsSDK(accId = accId,accToken=accToken,appId=appId)
# 准备数据
tid="1"
datas = ("%s"%sms_code,"%s"%expire) # 短信码,过期时间形参元组
# 发送短信
res = sdk.sendMessage(tid=tid,mobile=mobile,datas=datas)
# 解析json字符串
data = json.loads(res)
print("容联云响应的数据:",data)
return data
2.3 视图提交任务
# 短信验证码视图
import redis
from .tasks import send_message
class SmsCodeAPIView(APIView):
# 发送短信验证码
def get(self,request):
# 1.获取前端手机号
mobile = request.query_params.get("mobile")
# 生成随机码
sms_code = str(random.randint(10000,99999))
send_message.delay(sms_code,mobile) # 异步发送短信验证码
r = redis.Redis(host='127.0.0.1', port=6379, password='123123')
key = "sms_%s" % mobile
r.set(key, sms_code, ex=300)
return Response({
"code": 200, "msg": '短信已发送,请注意查收'})
2.4 启动celery
开两个终端,一个启动celery,一个启动django
celery -A p6 worker -l info -P eventlet
边栏推荐
- 开发必备之Idea使用
- Cadence learning path (VIII) PCB placement components
- 对NLP中transformer里面decoder的理解
- R language uses DALEX package to explain and analyze the machine learning model built by H2O package: summary and Practice
- Flask蓝图
- Cadence(九)17.4规则与间距设置
- 请求数据获取与响应
- Notes and Thoughts on the red dust of the sky (III) as long as the conditions are sufficient, the results will come naturally
- What does resource pooling and resource pooling mean?
- JDBC的学习以及简单封装
猜你喜欢

使用聚类分析 构建信用卡高风险客户识别模型

Filter in MATLAB

JDBC learning and simple encapsulation

6. Barycentric coordinate interpolation and graphics rendering pipeline

视、音频分开的网站内容如何合并?批量下载代码又该如何编写?

H1 -- HDMI interface test application 2022-07-15

Project deployment (simplified version)

項目部署(簡版)

An accident caused by MySQL misoperation, and "high availability" is not working well

Pyqt5 use qpainter to draw the coordinate axis and display the scatter diagram
随机推荐
Murata power maintenance switch server power maintenance and main functional features
TS type gymnastics intermediate type gymnastics challenge closing battle
Mysql database foundation
华为高层谈 35 岁危机,程序员如何破年龄之忧?
Huck hurco industrial computer maintenance winmax CNC machine tool controller maintenance
Filter in MATLAB
Activiti工作流使用之新建bpmn文件
十年架构五年生活-02第一份工作
Redis source code and design analysis -- 10. List object
使用pytorch实现基于VGG 19预训练模型的鲜花识别分类器,准确度达到97%
【无标题】
Cadence学习之路(八)PCB放置元器件
WebSocket长连接
JDBC learning and simple encapsulation
讲师征集令 | Apache DolphinScheduler Meetup分享嘉宾,期待你的议题和声音!
Master slave synchronization step read / write separation + self encountered error sharing
Activiti工作流使用之Activiti-app的安装及流程创建
LearnOpenGL - Introduction
Meyer burger Siemens industrial computer maintenance and machine tool maintenance
JDBC数据库连接池