python celery 无效值 -A 无法加载应用程序

python celery invalid value for -A unable to load application

提问人:Azima 提问时间:4/15/2021 更新时间:4/16/2021 访问量:17604

问:

我有一个以下项目目录:

azima:
    __init.py
    main.py
    tasks.py

task.py

from .main import app

@app.task
def add(x, y):
    return x + y

@app.task
def mul(x, y):
    return x * y

@app.task
def xsum(numbers):
    return sum(numbers)

main.py

from celery import Celery

app = Celery('azima', backend='redis://localhost:6379/0', broker='redis://localhost:6379/0', include=['azima.tasks'])

# Optional configuration, see the application user guide.
app.conf.update(
    result_expires=3600,
)

if __name__ == '__main__':
    app.start()

当我运行命令初始化芹菜工人时,出现以下错误:

(azima_venv) brightseid@darkseid:~$ celery -A azima worker -l INFO
Usage: celery [OPTIONS] COMMAND [ARGS]...

Error: Invalid value for '-A' / '--app': 
Unable to load celery application.
Module 'azima' has no attribute 'celery'

但是当我重命名为之前的样子时,没有问题。main.pycelery.py

我在这里错过了什么?

python 芹菜芹 菜任务

评论


答:

8赞 Patricio 4/16/2021 #1

有两种方法

  1. 将你的导入到appazima/__init__.py
from azima.main import app
    
celery = app  # you can omit this line

您可以省略最后一行,celery 将从导入中识别出芹菜应用程序。然后你打电话给celery -A azima worker -l INFO

  1. 称您为应用程序celery -A azima.main worker -l INFO