当前位置:网站首页>Cell sends SMS asynchronously
Cell sends SMS asynchronously
2022-07-23 11:16:00 【ZXY_ lucky】
1.Celery
install celery
celery, Distributed asynchronous task queue pip install celery==4.4.7
eventlet, Concurrent Network Library gevent Process library pip install eventlet==0.26.1
Celery It's based on python Distributed asynchronous task queue , The server submits the task ,celery Handle , The server sends the processed response to the client , It does not affect the operation of the following programs
- Asynchronous task , For example, sending text messages , Message push 、 Audio processing
- Do timed tasks
- have access to celery Implement a simple distributed crawler system
1.1 Celery analysis

- Task -- Mission
- Borker -- Message middleware , To receive tasks and send task information , Save task queue
- Worker– Background execution tasks , Monitor whether there are tasks in the queue , If there is one, execute it immediately
- Backend– Save task execution results
2. Send SMS asynchronously
2.1 To configure celery
Create a file under the project name celery.py file
from celery import Celery
from django.conf import settings
import os
# Configure environment variables
os.environ.setdefault("DJANGO_SETTINGS_MODULE",'p6.settings')
# Instantiation celery object
app = Celery("cc")
# Through the configuration file To configure
app.config_from_object("p6.config")
# Give Way app Auto discovery task function
app.autodiscover_tasks(settings.INSTALLED_APPS)
Create... Under this file config.py File store celery Information
broker_url = "redis://:[email protected]:6379/5"
result_backend = "redis://:[email protected]:6379/6"
2.2 Mission
Create under the sub application file tasks.py
from p6.celery import app
from ronglian_sms_sdk import SmsSDK
import json
accId = "8aaf07088185853e01818a75a87a016f"
accToken = "6b755e025dc2436a8e3b7b82ef9ac20c"
appId = "8aaf07088185853e01818a75a9a00176"
# Define the function of sending SMS
# Define task functions
@app.task
def send_message(sms_code,mobile,expire=5):
""" :param sms_code: The verification code to be sent :param mobile: Mobile number sent :param expire: Expiration time :return: """
# Instantiation sdk object
sdk = SmsSDK(accId = accId,accToken=accToken,appId=appId)
# Prepare the data
tid="1"
datas = ("%s"%sms_code,"%s"%expire) # SMS code , Expiration time formal parameter tuple
# Send a text message
res = sdk.sendMessage(tid=tid,mobile=mobile,datas=datas)
# analysis json character string
data = json.loads(res)
print(" Data responded by Ronglian cloud :",data)
return data
2.3 View submit task
# SMS verification code view
import redis
from .tasks import send_message
class SmsCodeAPIView(APIView):
# Send SMS verification code
def get(self,request):
# 1. Get the front-end mobile number
mobile = request.query_params.get("mobile")
# Generate random code
sms_code = str(random.randint(10000,99999))
send_message.delay(sms_code,mobile) # Send SMS verification code asynchronously
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": ' SMS sent , Please remember to check '})
2.4 start-up celery
Open two terminals , One boot celery, One boot django
celery -A p6 worker -l info -P eventlet
边栏推荐
- USCD行人异常数据集使用指南 | 快速下载
- Web server failed to start. Port 8080 was already in use.
- Data Lake: introduction to Apache iceberg
- Machine learning algorithm for large factory interview (6) time series analysis
- 【无标题】
- [Doris]配置和基本使用contens系统(有时间继续补充内容)
- 【系统问题】.NET Framework 3.5 安装错误
- Shardingsphere sub database and table scheme
- 3. Threads in flask
- 机器学习中的矩阵向量求导
猜你喜欢

Markdown common syntax records

【uiautomation】键指令大全(以及三种调用方式)+常用鼠标动作+SendKeys+Inspect学习
![[pytho-flask笔记5]蓝图简单使用](/img/0a/00b259f42e2fa83d4871263cc5f184.png)
[pytho-flask笔记5]蓝图简单使用

JDBC Learning and simple Encapsulation

【系统问题】.NET Framework 3.5 安装错误

Large factory interview machine learning algorithm (0): Feature Engineering | data preprocessing

Pycharm occupies C disk

【Anaconda 环境管理与包管理】

When using cache in sprintboot, the data cannot be loaded

plsql创建Oracle数据库报错:使用Database Control配置数据库时,要求在当前Oracle主目录中配置监听程序 必须运行Netca以配置监听程序,然后才能继续。或者
随机推荐
Scattered notes of machine learning: some concepts and notes
[Python flask note 5] Blueprint simple à utiliser
Mysql-8.0.28 user operation or user permission Operation
Leetcode daily question (1946. largest number after varying substring)
check the manual that corresponds to your MySQL server version for the right syntax to use near ‘ord
MySQL-8.0.28 用户操作 or 用户权限操作
Activiti工作流使用之Activiti-app的安装及流程创建
十年架构五年生活-01毕业之初
wps中的word中公式复制完后是图片
idea中复制一个项目/project
Powerbi Getting Started Guide
面试必备之数据库专题
安装企业版pycharm以及anaconda
[metric]使用Prometheus监控flink1.13org.apache.flink.metrics
通用视图,序列化器
【Pyradiomics】提取的影像组学特征值不正常(很多0和1)
Uscd pedestrian anomaly data set user guide | quick download
Gerrit 使用操作手册
【文献调研】在Pubmed上搜索特定影响因子期刊上的论文
[Anaconda environmental management and package management]