前言
flask + celery 启动celery服务时报错:
Unable to load celery application.
The module app.celery was not found.
问题描述
执行启动celery worker 命令
celery -A app.celery worker -l info
结果出现报错Unable to load celery application
解决方案
我的app.py代码
from flask import Flask
from celery import Celery
# 基本配置
broker_url = 'redis://localhost:6379'
result_backend = 'redis://localhost:6379'
app = Flask(__name__)
celery_app = Celery(app.import_name,
broker=broker_url,
backend=result_backend)
@celery_app.task(name='demo/add')
def add(x, y):
return x + y
由于我定义的Celery 实例名称是celery_app,所以启动的时候应用名称应该改成app.celery_app
-------------- celery@DESKTOP-HJ487C8 v5.2.7 (dawn-chorus)
--- ***** -----
-- ******* ---- Windows-10-10.0.17134-SP0 2022-09-08 11:11:14
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app: app:0x20e6cc88f40
[tasks]
. demo/add
启动日志看到[tasks]
说明正常启动了