Flask错误:无法加载Celery应用程序。

7

请帮我解决这个问题,我运行时出现了以下提示:

celery -A app.celery worker --loglevel=info

错误:

Unable to load celery application.
The module app.celery was not found.

我的代码是--

#  Celery Configuration
from celery import Celery
from app import app
print("App Name=",app.import_name)

celery=Celery(app.name,broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)

@celery.task
def download_content():
    return "hello"

目录结构-- newYoutube/app/auth/routes.py,此函数在routes.py中。 auth是蓝图。

2个回答

10

当通过celery调用时

celery -A app.celery ...

app命名空间中,celery将查找名称celery,期望其中包含一个Celery实例。如果您将其放在其他位置(例如app.auth.routes),则celery将无法找到它。

我有一个可供借鉴的工作示例,在https://github.com/davewsmith/flask-celery-starter上可以找到。

或者,参考Flask Mega教程的第22章,该教程使用rx而不是celery,但代码结构的一般方法类似。


0
如果您正在使用create_app函数,从该函数中返回celery和app两个变量。例如:
def create_app():
    app = Flask(__name__)
    
    # app config ...

    celery=Celery(app.name,broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)

    return app, celery

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