Python Celery:-A选项的无效值,无法加载应用程序。

7

我有一个如下的项目目录:

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()

当我运行初始化celery worker的命令时,出现以下错误:

(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.py重命名为之前的celery.py时,就没有问题了。

我错过了什么?

1个回答

8

有两种方法:

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

您可以省略最后一行代码,celery将从导入中识别到Celery应用程序。然后,您可以调用 celery -A azima worker -l INFO

  1. 像这样调用您的应用程序:celery -A azima.main worker -l INFO

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接