【问题标题】:python celery invalid value for -A unable to load applicationpython celery 无效值 -A 无法加载应用程序
【发布时间】:2021-07-10 11:54:03
【问题描述】:

我有以下项目目录:

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 时没有问题。

我在这里错过了什么?

【问题讨论】:

    标签: python celery celery-task


    【解决方案1】:

    有两种方法

    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这样称呼你的应用程序

    【讨论】:

      猜你喜欢
      • 2021-04-08
      • 2020-03-11
      • 2021-05-13
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      • 1970-01-01
      相关资源
      最近更新 更多