当前位置:网站首页>手机异步发送短信验证码解决方案-Celery+redis
手机异步发送短信验证码解决方案-Celery+redis
2022-07-04 00:34:00 【Mr_WoLong】
Celery介绍和使用
一.Celery介绍:
- 一个简单、灵活且可靠、处理大量消息的分布式系统,可以在一台或者多台机器上运行。 Celery是一个功能完备即插即用的任务队列
- 单个 Celery 进程每分钟可处理数以百万计的任务。通过消息进行通信,使用消息队列(broker)在客户端和消费者之间进行协调。
安装Celery:
$ pip install -U Celery
Celery官方文档:https://docs.celeryq.dev/en/latest/index.html
1. 生产者消费者设计模式
最常用的解耦方式之一,寻找中间人(broker)搭桥,保证两个业务没有直接关联。
我们称这一解耦方式为:生产者消费者设计模式
总结:
- 生产者生成消息,缓存到消息队列中,消费者读取消息队列中的消息并执行。
- 由美多商城生成发送短信消息,缓存到消息队列中,消费者读取消息队列中的发送短信消息并执行。
2.中间人broker
示例:此处演示Redis数据库作为中间人broker
Celery需要一种解决消息的发送和接受的方式,我们把这种用来存储消息的的中间装置叫做message broker, 也可叫做消息中间人。
作为中间人,我们有几种方案可选择:
1.RabbitMQ
- RabbitMQ是一个功能完备,稳定的并且易于安装的broker. 它是生产环境中最优的选择。
使用RabbitMQ的细节参照以下链接:http://docs.celeryproject.org/en/latest/getting-started/brokers/rabbitmq.html#broker-rabbitmq
如果使用的是Ubuntu或者Debian发行版的Linux,可以直接通过命令安装RabbitMQ:
sudo apt-get install rabbitmq-server
安装完毕之后,RabbitMQ-server服务器就已经在后台运行。
如果用的并不是Ubuntu或Debian, 可以在以下网址:
http://www.rabbitmq.com/download.html
去查找自己所需要的版本软件。
2.Redis
Redis也是一款功能完备的broker可选项,但是其更可能因意外中断或者电源故障导致数据丢失的情况。
关于是由那个Redis作为Broker,可访下面网址: http://docs.celeryproject.org/en/latest/getting-started/brokers/redis.html#broker-redis
3.celery框架伪代码
# 队列,中间人
class Broker(object):
# 任务队列
broker_list = []
# 消费者
class Worker(object):
# 任务执行者
def run(self, broker, func):
if func in broker.broker_list:
func()
else:
return 'error'
# Celery 将这3者 串联起来了
class Celery(object):
def __init__(self):
self.broker = Broker()
self.worker = Worker()
def add(self, func):
self.broker.broker_list.append(func)
def work(self, func):
self.worker.run(self.broker,func)
# 任务(函数),生产者
def send_sms_code():
print('send_sms_code')
# 1.创建celery实例
app=Celery()
# 2. 添加任务
app.add(send_sms_code)
# 3.执行任务
app.work(send_sms_code)
2.使用
1)创建Celery实例并加载配置
1.定义Celery包
2.创建Celery实例
mian文件代码celery_tasks.main.py
# 0. 为celery的运行 设置Django的环境
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'meiduo_mall.settings')
# 1. 创建celery实例
from celery import Celery
# 参数1: main 设置脚本路径就可以了。 脚本路径是唯一的
app=Celery('celery_tasks')
#2. 设置broker
# 我们通过加载配置文件来设置broker
app.config_from_object('celery_tasks.config')
#3.需要celery 自动检测指定包的任务
# autodiscover_tasks 参数是列表
# 列表中的元素是 tasks的路径
app.autodiscover_tasks(['celery_tasks.sms','celery_tasks.email'])
3.加载Celery配置
代码config.py
broker_url = "redis://127.0.0.1/15"
###########################说明#############################
# 如果使用别的作为中间人, 例如使用 rabbitmq
# 则 rabbitmq 配置如下:
# broker_url= 'amqp://用户名:密码@ip地址:5672'
# 例如:
# meihao: 在rabbitq中创建的用户名, 注意: 远端链接时不能使用guest账户.
# 123456: 在rabbitq中用户名对应的密码
# ip部分: 指的是当前rabbitq所在的电脑ip
# 5672: 是规定的端口号
# broker_url = 'amqp://meihao:[email protected]:5672'
2)定义发送短信任务
1.注册任务:celery_tasks.main.py
# 自动注册celery任务
#3.需要celery 自动检测指定包的任务
# autodiscover_tasks 参数是列表
# 列表中的元素是 tasks的路径
app.autodiscover_tasks(['celery_tasks.sms','celery_tasks.email'])
2.定义任务:celery_tasks.sms.tasks.py
task.py
代码
# 生产者 -- 任务,函数
# 1. 这个函数 必须要让celery的实例的 task装饰器 装饰
# 2. 需要celery 自动检测指定包的任务
from libs.yuntongxun.sms import CCP
from celery_tasks.main import app
@app.task
def celery_send_sms_code(mobie,code):
CCP().send_template_sms(mobie,[code,5],1)
示例demo
from celery_tasks.main import celery_app
from libs.yuntongxun.sms import CCP
import logging
logger = logging.getLogger('django')
# name:异步任务别名
@celery_app.task(name='send_sms_code')
def send_sms_code(mobile, sms_code):
""" 发送短信异步任务 :param mobile: 手机号 :param sms_code: 短信验证码 """
try:
send_ret = CCP().send_template_sms(mobile, [sms_code, 5], 1)
except Exception as e:
logger.error(e)
4)启动Celery服务
进入虚拟环境,输入命令:$ celery -A celery_tasks.main worker -l INFO
- -A指对应的应用程序, 其参数是项目中 Celery实例的位置。
- worker指这里要启动的worker。
- -l指日志等级,比如info等级。
5)调用发送短信任务
发送短信业务逻辑代码
from celery_tasks.sms.tasks import celery_send_sms_code
# delay 的参数 等同于 任务(函数)的参数
celery_send_sms_code.delay(mobile,sms_code)
示例demo
# 发送短信验证码
# CCP().send_template_sms(mobile,[sms_code, 5], 1)
# Celery异步发送短信验证码
send_sms_code.delay(mobile, sms_code)
6)补充celery worker的工作模式
- 默认是进程池方式,进程数以当前机器的CPU核数为参考,每个CPU开四个进程。
- 如何自己指定进程数: celery worker -A proj --concurrency=4
- 如何改变进程池方式为协程方式: celery worker -A proj --concurrency=1000 -P eventlet
-c 1000
安装eventlet模块: $ pip install eventlet
启用 Eventlet 池: $ celery -A celery_tasks.main worker -l info -P eventlet -c 1000
边栏推荐
- URL (data:image/png; Base64, ivborw0k... Use case
- Advanced C language - pointer 2 - knowledge points sorting
- It is forbidden to splice SQL in code
- Similarities and differences of text similarity between Jaccard and cosine
- [PHP basics] session basic knowledge, application case code and attack and defense
- It is worthy of "Alibaba internal software test interview notes" from beginning to end, all of which are essence
- Selenium library 4.5.0 keyword explanation (4)
- [Mongodb] 2. Use mongodb --------- use compass
- How will the complete NFT platform work in 2022? How about its core functions and online time?
- The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
猜你喜欢
Solve the problem that the kaggle account registration does not display the verification code
Analysis: misunderstanding of choosing WMS warehouse management system
STM32 GPIO CSDN creative punch in
Pytest unit test framework: simple and easy to use parameterization and multiple operation modes
URL (data:image/png; Base64, ivborw0k... Use case
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Self study software testing. To what extent can you go out and find a job?
Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method
From functional testing to automated testing, how did I successfully transform my salary to 15K +?
Zipper table in data warehouse (compressed storage)
随机推荐
It is worthy of "Alibaba internal software test interview notes" from beginning to end, all of which are essence
MySQL is installed as a Windows Service
A dichotomy of Valentine's Day
[Mongodb] 2. Use mongodb --------- use compass
UTS | causal reasoning random intervention based on Reinforcement Learning
How to make recv have a little temper?
P1629 postman delivering letter
Global and Chinese markets for instant saliva testing devices 2022-2028: Research Report on technology, participants, trends, market size and share
Analysis on the scale of China's smart health industry and prediction report on the investment trend of the 14th five year plan 2022-2028 Edition
Make small tip
How to be a professional software testing engineer? Listen to the byte five year old test
Several ways to set up a blog locally [attach relevant software download links]
Anomalies seen during the interview
Development and application of fcitx functional plug-ins
Celebrate the new year | Suihua fire rescue detachment has wonderful cultural activities during the Spring Festival
Axure resources and prototype tool Axure RP 9 download
2020.2.14
Self study software testing. To what extent can you go out and find a job?
[error record] configure NDK header file path in Visual Studio
Report on the construction and development mode and investment mode of sponge cities in China 2022-2028