Celery Django- using asynchronous send SMS codes

  • When Django encountered in the implementation of time-consuming operations, such as third-party request to send text messages, in order not to block the process, it should be time-consuming code is decoupled from the main business (using producer-consumer model)


    16394388-9b6d457efdd787d1.png
    Consumers creator
  • In Django Celery can be used to complete asynchronous, we use producer-consumer model, only we need to focus on the task itself, which greatly simplifies the programmer's development process.

  • Redis RabbitMQ or may be used as a container message queue

What is Celery

Introduction:

  • A simple, flexible and reliable, a large number of messages distributed processing system that can run on one or multiple machines.
  • Celery single process to handle the task of millions per minute.
  • Communicate by message, using the message queue (Broker) coordination between the client and the consumer.
    Mounting
  • pip install -U Celery

Celery send text messages using the defined task

  1. In the project root directory, create Celery manage.py package under the same directory, such as named celery_tasks
  2. Main.py file created in celery_tasks package, create Celery instance.
# celery启动文件
from celery import Celery


# 创建celery实例
celery_app = Celery('meiduo')
  1. Celery is loaded configuration file is created in the celery_tasks config.py package, specify the message queue position, I used here is Redis, RabbitMQ better use
# 指定消息队列的位置
# 这里使用redis作为队列容器
broker_url = "redis://127.0.0.1:6379/12"
  1. Define Send SMS task: create a task package sms in celery_tasks package, to create a task file within the package tasks.py
    special attention: tasks.py life filename must do this name, can not customize other name! !
# bind:保证task对象会作为第一个参数自动传入
# name:异步任务别名
# retry_backoff:异常自动重试的时间间隔 第n次(retry_backoff×2^(n-1))s
# max_retries:异常自动重试次数的上限
from meiduo_mall.libs.yuntongxun.sms import CCP
# constants为自定义的过期时间配置文件
from celery_tasks.sms import constants
from celery_tasks.main import app

# 使用装饰器装饰任务
@app.task(bind=True, name="send_sms", retry_backoff=3)
def send_sms(self,mobile,sms_code):
    """
    异步发送短信任务
    :param self:
    :param mobile:手机号
    :param sms_code: 短信验证码
    :return: 成功0,失败-1
    """
    # 将耗时的代码封装在一个方法中
    ccp = CCP()
    ret = ccp.send_template_sms(mobile, [sms_code,constants.SMS_CODE_EXPIRES],1)
    if ret != 0:
         raise self.retry(exe=Exception("发送短信失败"), max_retries=3)
    return ret
  1. Sign up tasks main.py file
# celery启动文件
from celery import Celery


# 创建celery实例
celery_app = Celery('meiduo')
# 加载celery配置
celery_app.config_from_object('celery_tasks.config')
# 自动注册celery任务
celery_app.autodiscover_tasks(['celery_tasks.sms'])

Celery task definition is complete, and only need to call the task at the specified location:

Celery task and open calls

  1. Start Celery service:
    the implementation of the project root directory
    celery -A celery_tasks.main worker -l info
    • -A means corresponding to an application, which is a project parameter Celery instances position.
    • worker refers here to start worker.
    • -l refers to the log level, such as info grade.
  2. Send text messages to call the task
    at the specified location, perform the task Celery, referring to the article:
    Django- implement CAPTCHA, SMS verification code function (b)
ccp_send_sms_code.delay(mobile, sms_code)

The task will be executed asynchronously without blocking the subsequent code execution.

Guess you like

Origin blog.csdn.net/weixin_34146986/article/details/90786463