当前位置:网站首页>Mobile asynchronous sending SMS verification code solution -efficiency+redis
Mobile asynchronous sending SMS verification code solution -efficiency+redis
2022-07-04 00:40:00 【Mr_ WoLong】
Celery Introduction and use
One .Celery Introduce :
- A simple 、 Flexible and reliable 、 A distributed system that processes a large number of messages , Can run on one or more machines . Celery Is a fully functional plug and play task queue
- Single Celery Processes can handle millions of tasks per minute . Communicating through messages , Using message queuing (broker) Coordinate between clients and consumers .
install Celery:
$ pip install -U Celery
Celery Official documents :https://docs.celeryq.dev/en/latest/index.html
1. Producer consumer design pattern
One of the most commonly used decoupling methods , Find a middleman (broker) Bridging , Ensure that the two businesses are not directly related .
We call this decoupling method : Producer consumer design pattern
summary :
- Producers generate messages , Cache to message queue , The consumer reads the message in the message queue and executes .
- Send SMS messages generated by Mido mall , Cache to message queue , The consumer reads the SMS message sent in the message queue and executes the message .
2. A middleman broker
Example : Here's a demo of Redis The database acts as an intermediary broker
Celery We need a way to send and receive messages , We call this intermediate device used to store messages message broker, It can also be called a message broker .
As a middleman , We have several options :
1.RabbitMQ
- RabbitMQ Is a fully functional , Stable and easy to install broker. It is the best choice in the production environment .
Use RabbitMQ For details, please refer to the following links :http://docs.celeryproject.org/en/latest/getting-started/brokers/rabbitmq.html#broker-rabbitmq
If you are using Ubuntu perhaps Debian Distribution Linux, You can install directly through the command RabbitMQ:
sudo apt-get install rabbitmq-server
After installation ,RabbitMQ-server The server is already running in the background .
If not Ubuntu or Debian, You can visit the following website :
http://www.rabbitmq.com/download.html
Find the version of software you need .
2.Redis
Redis It is also a fully functional broker optional , But it is more likely to cause data loss due to accidental interruption or power failure .
About that Redis As Broker, Visit the following website : http://docs.celeryproject.org/en/latest/getting-started/brokers/redis.html#broker-redis
3.celery Framework pseudocode
# queue , A middleman
class Broker(object):
# Task queue
broker_list = []
# consumer
class Worker(object):
# Task executor
def run(self, broker, func):
if func in broker.broker_list:
func()
else:
return 'error'
# Celery Will this 3 person It's connected in series
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)
# Mission ( function ), producer
def send_sms_code():
print('send_sms_code')
# 1. establish celery example
app=Celery()
# 2. Add tasks
app.add(send_sms_code)
# 3. Perform tasks
app.work(send_sms_code)
2. Use
1) establish Celery Instance and load the configuration
1. Definition Celery package
2. establish Celery example
mian File code celery_tasks.main.py
# 0. by celery Operation of Set up Django Environment
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'meiduo_mall.settings')
# 1. establish celery example
from celery import Celery
# Parameters 1: main Just set the script path . The script path is unique
app=Celery('celery_tasks')
#2. Set up broker
# We set by loading the configuration file broker
app.config_from_object('celery_tasks.config')
#3. need celery Automatically detect the task of the specified package
# autodiscover_tasks The parameter is the list
# The elements in the list are tasks The path of
app.autodiscover_tasks(['celery_tasks.sms','celery_tasks.email'])
3. load Celery To configure
Code config.py
broker_url = "redis://127.0.0.1/15"
########################### explain #############################
# If you use someone else as an intermediary , For example, using rabbitmq
# be rabbitmq The configuration is as follows :
# broker_url= 'amqp:// user name : password @ip Address :5672'
# for example :
# meihao: stay rabbitq User name created in , Be careful : Cannot use... When remote link guest Account .
# 123456: stay rabbitq The password corresponding to the user name in
# ip part : It means the present rabbitq The computer where you are ip
# 5672: Is the specified port number
# broker_url = 'amqp://meihao:[email protected]:5672'
2) Define the task of sending SMS
1. Registration task :celery_tasks.main.py
# Automatic registration celery Mission
#3. need celery Automatically detect the task of the specified package
# autodiscover_tasks The parameter is the list
# The elements in the list are tasks The path of
app.autodiscover_tasks(['celery_tasks.sms','celery_tasks.email'])
2. Define the task :celery_tasks.sms.tasks.py
task.py
Code
# producer -- Mission , function
# 1. This function Must let celery Instance task Decorator decorate
# 2. need celery Automatically detect the task of the specified package
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)
Example demo
from celery_tasks.main import celery_app
from libs.yuntongxun.sms import CCP
import logging
logger = logging.getLogger('django')
# name: Asynchronous task alias
@celery_app.task(name='send_sms_code')
def send_sms_code(mobile, sms_code):
""" Send SMS asynchronous task :param mobile: cell-phone number :param sms_code: SMS verification code """
try:
send_ret = CCP().send_template_sms(mobile, [sms_code, 5], 1)
except Exception as e:
logger.error(e)
4) start-up Celery service
Enter the virtual environment , Enter the command :$ celery -A celery_tasks.main worker -l INFO
- -A Refers to the corresponding application , Its parameters are in the project Celery The location of the instance .
- worker It refers to the... To be started here worker.
- -l Refers to the log level , such as info Grade .
5) Call send SMS task
Send SMS business logic code
from celery_tasks.sms.tasks import celery_send_sms_code
# delay Parameters of Equate to Mission ( function ) Parameters of
celery_send_sms_code.delay(mobile,sms_code)
Example demo
# Send SMS verification code
# CCP().send_template_sms(mobile,[sms_code, 5], 1)
# Celery Send SMS verification code asynchronously
send_sms_code.delay(mobile, sms_code)
6) Add celery worker Working mode of
- The default is process pool mode , The number of processes on the current machine CPU For reference , Every CPU Open four processes .
- How to specify the number of processes : celery worker -A proj --concurrency=4
- How to change the process pool mode to co process mode : celery worker -A proj --concurrency=1000 -P eventlet
-c 1000
install eventlet modular : $ pip install eventlet
Enable Eventlet pool : $ celery -A celery_tasks.main worker -l info -P eventlet -c 1000
边栏推荐
- Is the account opening of Guoyuan securities really safe and reliable
- 不得不会的Oracle数据库知识点(四)
- Alibaba test engineer with an annual salary of 500000 shares notes: a complete set of written tests of software testing
- Cannot build artifact 'test Web: War expanded' because it is included into a circular depend solution
- Entropy and full connection layer
- BBS forum recommendation
- How to use AHAS to ensure the stability of Web services?
- Private project practice sharing populate joint query in mongoose makes the template unable to render - solve the error message: syntaxerror: unexpected token r in JSON at
- Several ways to set up a blog locally [attach relevant software download links]
- OS interrupt mechanism and interrupt handler
猜你喜欢
功能:编写函数fun求s=1^k+2^k +3^k + ......+N^k的值, (1的K次方到N的K次方的累加和)。
On covariance of array and wildcard of generic type
A method to solve Bert long text matching
(Introduction to database system | Wang Shan) Chapter V database integrity: Exercises
From functional testing to automated testing, how did I successfully transform my salary to 15K +?
NLP pre training technology development
Recommendation of knowledge base management system
1-Redis架构设计到使用场景-四种部署运行模式(上)
[NLP] text classification still stays at Bert? Duality is too strong than learning framework
功能:将主函数中输入的字符串反序存放。例如:输入字符串“abcdefg”,则应输出“gfedcba”。
随机推荐
MySQL 8.0.12 error: error 2013 (HY000): lost connection to MySQL server during query
Makefile judge custom variables
Suggestions for improving code quality
功能:求5行5列矩阵的主、副对角线上元素之和。注意, 两条对角线相交的元素只加一次。例如:主函数中给出的矩阵的两条对角线的和为45。
What is regression testing? Talk about regression testing in the eyes of Ali Test Engineers
[error record] configure NDK header file path in Visual Studio (three header file paths of NDK | ASM header file path selection related to CPU architecture)
Detailed explanation of the relationship between Zhongtai, wechat and DDD
Analysis and solution of lazyinitializationexception
A-Frame虚拟现实开发入门
[CSDN Q & A] experience and suggestions
基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
[software testing] you haven't mastered these real interview questions of big companies?
ITK learning notes (VII) the position of ITK rotation direction remains unchanged
Severity code description the project file line prohibits the display of status error c4996 fopen ('fscanf ', StrCmp): this function or variable may be unsafe The most comprehensive solution
数据库表外键的设计
[C language] break and continue in switch statement
What insurance products should be bought for the elderly?
1-Redis架构设计到使用场景-四种部署运行模式(上)
leetcode 121 Best Time to Buy and Sell Stock 买卖股票的最佳时机(简单)
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)