main.py文件
pip3 install -U Celery
from celery import Celery
from ihome.tasks import config
celery_app = Celery("ihome")
celery_app.config_from_object(config)
celery_app.autodiscover_tasks(["ihome.tasks.sms"])
config.py文件
BROKER_URL = "redis://127.0.0.1:6379/1"
CELERY_RESULT_BACKEND = "redis://127.0.0.1:6379/2"
tasks.py文件
from ihome.tasks.main import celery_app
from ihome.libs.sms import send_sms_single
@celery_app.task
def send_sms(mobile, datas, sms_code):
"""发送短信的异步任务"""
sms = send_sms_single(mobile, datas, [sms_code, ])
return sms
目录结构
celery -A ihome.tasks.main worker