celery的简单使用

安装

pip install celery

创建项目

 config.py的内容

# 设置任务队列的url地址
broker_url = "redis://:88888888@127.0.0.1:6379/14"
# 设置结果队列的url地址
result_backend = "redis://:88888888@127.0.0.1:6379/15"

main.py中的内容

from celery import Celery

# 创建celery主程序对象
app = Celery()# 加载配置
app.config_from_object("mycelery.config")

# 注册任务会自动到app目录中查找tasks.py
app.autodiscover_tasks(["mycelery.sms","mycelery.mail"])

# 通过终端来启动celery
# celery -A mycelery.main worker --loglevel=info

tasks.py中的内容

from mycelery.main import app


@app.task(name='sms')
def send_sms(phone):
    return f"给{phone}发送短信"

启动celery的命令

celery -A mycelery.main worker --loglevel=info

给celery发送任务

# 发送 celery 任务

from mycelery.sms.tasks import send_sms

send_sms.delay('12345678900')
原文地址:https://www.cnblogs.com/wtil/p/14956665.html