1.celery配置与使用
1.1 安装celery
pip install celery @ https://github.com/celery/celery/tarball/master
1.2 新建celery/main.py
配置celery
# celery_task/main.py
# -*- coding: utf-8 -*-
from celery import Celery
import os,sys
import django
from datetime import timedelta
# # 1.添加django项目根路径
CELERY_BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(CELERY_BASE_DIR, '../opwf'))
# 2.添加django环境
os.environ.setdefault("DJANGO_SETTINGS_MODULE","opwf.settings")
django.setup() # 读取配置
# 3.celery基本配置
app = Celery('mycelery',
broker='redis://localhost:6379/14',
backend='redis://localhost:6379/15',
include=['celery_task.tasks',
'celery_task.tasks2',
])
# 4.实例化时可以添加下面这个属性
app.conf.update(
result_expires=3600, #执行结果放到redis里,一个小时没人取就丢弃
)
# 5.配置定时任务:每5秒钟执行 调用一次celery_pro下tasks.py文件中的add函数
app.conf.beat_schedule = {
'add-every-5-seconds': {
'task': 'tasks.test_task_crontab',
'schedule': timedelta(seconds=5),
'args': (16, 16)
},
}
# 6.添加时区配置
app.conf.timezone = 'UTC'
if __name__ == '__main__':
app.start()
celery_task/main.py
1.3创建task文件来定义异步任务
# -*- coding:utf8 -*-
from .main import app #从当前目录导入app
import os,sys
from .main import CELERY_BASE_DIR
# 1.test_task_crontab测试定时任务
@app.task
def test_task_crontab(x,y):
# 添加django项目路径
# sys.path.insert(0, os.path.join(CELERY_BASE_DIR, '../opwf'))
from utils.rl_sms import test_crontab
res = test_crontab(x, y)
print(x+y)
return x + y
# 2.测试异步发送邮件
@app.task(bind=True)
def send_sms_code(self, mobile, datas):
sys.path.insert(0, os.path.join(CELERY_BASE_DIR, '../opwf'))
# 在方法中导包
from utils.rl_sms import send_message
# time.sleep(5)
try:
# 用 res 接收发送结果, 成功是:0, 失败是:-1
res = send_message(mobile, datas)
except Exception as e:
res = '-1'
if res == '-1':
# 如果发送结果是 -1 就重试.
self.retry(countdown=5, max_retries=3, exc=Exception('短信发送失败'))
celery_task/tasks.py
1.4封装一个公用的方法
# -*- coding: utf-8 -*-
# utils/rl_sms.py
from ronglian_sms_sdk import SmsSDK
from user.models import User
accId = '8a216da8747ac98201749c0de38723b7'
accToken = '86072b540b4648229b27400414150ef2'
appId = '8a216da8747ac98201749c0de45123be'
def send_message(phone, datas):
user = User.objects.all()[0]
print(user.username, '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
sdk = SmsSDK(accId, accToken, appId)
tid = '1' # 测试模板id为: 1. 内容为: 【云通讯】您的验证码是{1},请于{2}分钟内正确输入。
# mobile = '13303479527'
# datas = ('666777', '5') # 模板中的参数按照位置传递
# resp = sdk.sendMessage(tid, phone, datas)
print("##########################################")
print('执行了这个方法 send_message')
return ''
from workorder.models import SubOrder
def test_crontab(x,y):
# need_approve = SubOrder.objects.filter(suborder_status='1')
print('############### 执行test_crontab测试任务 #############')
print(x+y)
print('############### 邮件审批超时提醒 #############')
opwf/utils/rl_sms.py
1.5在views.py里使用异步任务来处理函数
import sys,os
from opwf.settings import BASE_DIR
from django.http.response import JsonResponse
# Create your views here.
def test(request):
print(BASE_DIR)
#添加总文件opwf_project文件夹为新的导包路径,将celery_task中的文件导入
sys.path.insert(0, os.path.join(BASE_DIR, '../../opwf_project'))
print(sys.path)
#导入celery_task文件中的异步任务
from celery_task.tasks import test_task_crontab
#异步任务的调用
test_task_crontab.delay(10,20)
return JsonResponse({"code":200})
views.py
2.测试celery
2.1 启动celery
'''1.启动celery'''
#1.1 单进程启动celery
celery -A main worker -l INFO
#1.2 celery管理
celery multi start celery_test -A celery_test -l debug --autoscale=50,5 # celery并发数:最多50个,最少5个
ps auxww|grep "celery worker"|grep -v grep|awk '{print $2}'|xargs kill -9 # 关闭所有celery进程