安装pip install -U Celery
一、celery_tasks.main.pyfrom celery import Celery# 创建celery实例celery_app = Celery('meiduo')# 添加消息队列(中间人broker)celery_app.config_from_object('celery_tasks.config')# 添加celery任务celery_app.autodiscover_tasks(['celery_tasks.sms'])
二、celery_tasks.sms.tasks.pyfrom celery_tasks.main import celery_appfrom celery_tasks.sms.yuntongxun.ccp_sms import CCPfrom celery_tasks.sms import constants@celery_app.task(name='send_sms_code')def send_sms_code(mobile, sms_code): """ :param self: :param mobile: :param sms_code: :return: """ send_ret = CCP().send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES // 60], constants.SEND_SMS_TEMPLATE_ID) return send_ret
三、verifications.view.py中添加发送信息语句# 使用celery发送短信验证码:from celery_tasks.sms.tasks import send_sms_code send_sms_code.delay(mobile, sms_code)
四、执行terminal中虚拟环境下meiduo_mall路径中执行语句:celery -A celery_tasks.main worker -l info -P eventlet