1 2 3 4 5 6 7 8 9 10 11 | # main.py from celery import Celery # 创建Celery类对象 celery_app = Celery('demo') # 加载配置信息 celery_app.config_from_object('配置文件包路径') # celery worker启动时自动发现任务函数 celery_app.auto_discover_tasks([...]) |
1 2 3 4 | # config.py # 设置中间人地址 # broker_url = 'redis://<ip>:<port>/<db>' broker_url = 'redis://172.16.179.139:6379/3' |
1 2 3 | @celery_app.task(name='send_sms_code') def send_sms_code(a, b): # 任务函数的代码... |
from celery import Celery
# 创建Celery类实例对象
celery_app = Celery('celery_tasks')
# 加载配置
celery_app.config_from_object('celery_tasks.config')
# celery worker启动时自动发现任务函数
celery_app.autodiscover_tasks(['celery_tasks.sms'])
# 封装发送短信的任务函数
from celery_tasks.main import celery_app
from celery_tasks.sms.yuntongxun.sms import CCP
# 发送短信模板id
SEND_SMS_TEMP_ID = 1
# 获取日志器
import logging
logger = logging.getLogger('django')
@celery_app.task(name='send_sms_code')
def send_sms_code(mobile, sms_code, expires):
print('短信发送任务函数被调用...')
try:
res = CCP().send_template_sms(mobile, [sms_code, expires], SEND_SMS_TEMP_ID)
except Exception as e:
logger.error('发送短信异常: mobile: %s sms_code: %s' % (mobile, sms_code))
else:
if res != 0:
# 发送短信失败
logger.error('发送短信失败: mobile: %s sms_code: %s' % (mobile, sms_code))
else:
# 发送短信成功
logger.info('发送短信成功: mobile: %s sms_code: %s' % (mobile, sms_code))
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |