Celery使用

推荐版本  
  Django == 2.2.6
  django-celery == 3.3.1
  django-redis == 4.11.0
  redis == 2.10.6
  celery == 3.1.26.post2
Settings.py
创建celery所需要的表
创建task
  在app⾥里里建⽴立tasks.py⽂文件来写⼊入需要执⾏行行的异步任务
#settings.py
import djcelery
djcelery.setup_loader()
BROKER_URL = 'redis://127.0.0.1:6379/2'
INSTALLED_APPS = [
...
"djcelery",
...
]
python manage.py migrate
#如若不不成功可以尝试⼀一下命令语句句
#python manage.py syncdb
###############verificationsapp/tasks.py########
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from code2001B.settings import ALY_ACCESSKEY_ID,ALY_ACCESSKEY_SECRET
import json,time
from celery import task
@task
def send_sms(phone,data):
# client = AcsClient(ALY_ACCESSKEY_ID, ALY_ACCESSKEY_SECRET, 'cn-
hangzhou')
#
# #data ={ "code" : 123456 }
#
# request = CommonRequest()
# request.set_accept_format('json')
# request.set_domain('dysmsapi.aliyuncs.com')
# request.set_method('POST')
# request.set_protocol_type('https') # https | http
# request.set_version('2017-05-25')
a、当settings.py中的djcelery.setup_loader()运⾏行行时, Celery便便会查看所有INSTALLED_APPS中app
⽬目录中的tasks.py⽂文件, 找到标记为task的function, 并将它们注册为celery task.
  b、在执⾏行行djcelery.setup_loader()时, task是以INSTALLED_APPS中的app名,
加.tasks.function_name注册的
  c、⼀一次需要注意 在impprt task时, 需要保持⼀一致
  d、如果我们由于python path不不同⽽而使⽤用不不同的引⽤用⽅方式时(例例如在tasks.py中使⽤用from
myproject.myapp.tasks import add形式), Celery将⽆无法得知这是同⼀一task, 因此可能会引起奇怪的
bug。
views.py⾥里里让任务异步执⾏行行
# request.set_action_name('SendSms')
#
# request.add_query_param('RegionId', "cn-hangzhou")
# request.add_query_param('PhoneNumbers', phone)
# request.add_query_param('SignName', "美多商城")
# request.add_query_param('TemplateCode', "SMS_185212884")
# request.add_query_param('TemplateParam', data)
# response1 = client.do_action(request)
# # python2: print(response)
# res = json.loads(str(response1, encoding='utf-8'))
time.sleep(5)
return 5+10

 

from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from libs.captcha.captcha import captcha
from django.http.response import HttpResponse
import random
from verificationsapp.tasks import send_sms
from django_redis import get_redis_connection
class SendSMSCode(APIView):
def post(self,request):
phone = request.data.get("phone")
image_code = request.data.get("image_code")
image_code_uuid = request.data.get("image_code_uuid")
print(phone)
print(image_code_uuid)
print(image_code)
启动celery
  ⾸首先正常启动你的django任务,然后启动celery服务即可。
 如果报错不不让超级管理理员来启动,在settings.py加⼊入以下配置
3、注册功能的完善
if not all([phone,image_code,image_code_uuid]):
return Response({"code":4005,"msg":"参数不不全"})
#先获取redis ⾥里里的图⽚片验证码来⽐比对
redis_cli = get_redis_connection("img_code")
redis_img_code = redis_cli.get(image_code_uuid).decode()
print(redis_img_code)
print(image_code)
if image_code.lower() != redis_img_code.lower():
return Response({"code":4003,"msg":"参数错误"})
#发送短信
num = random.randint(100000,999999)
print(num)
send_data = {"code":10086}
send_sms.delay(phone,send_data)
# 删除redis⾥里里的image_code,保存phone_code
#pipeline管道:作⽤用就是把多个命令放在⼀一起来执⾏行行
pl = redis_cli.pipeline()
pl.setex(phone,60*5,num)
pl.delete(image_code_uuid)
pl.execute()
return Response({"code":0,"msg":"发送成功"})

上一篇:celery异步发短信


下一篇:flask项目1实战:2.5 Celery介绍和使用(待完善)