flask celery work报错KeyError: 'app.tasks.send_async_email'

发布于 2022-09-12 01:10:13 字数 2052 浏览 10 评论 0

这个问题小弟看了好多天了,在网上搜索了各种答案,尝试了很多次依旧没解决,还请劳烦各位大佬帮忙看看。

依赖包:
celery==4.4.0
Flask==1.1.1

项目结构如下:
image.png

route.py 中提交异步任务
tasks.py 中存放异步函数
image.png

结果报错如下:

## celery -A app:celery worker -B -E --loglevel=INFO 
 
 -------------- celery@felixdeMacBook-Pro.local v4.4.0 (cliffs)
--- ***** ----- 
-- ******* ---- Darwin-19.3.0-x86_64-i386-64bit 2020-02-23 18:31:40
- *** --- * --- 
- ** ---------- [config]
- ** ---------- .> app:         app:0x105bf0518
- ** ---------- .> transport:   redis://:**@localhost:6379/0
- ** ---------- .> results:     disabled://
- *** --- * --- .> concurrency: 8 (prefork)
-- ******* ---- .> task events: ON
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery
                

[tasks]


[2020-02-23 18:31:41,293: INFO/Beat] beat: Starting...
[2020-02-23 18:31:41,294: INFO/MainProcess] Connected to redis://:**@localhost:6379/0
[2020-02-23 18:31:41,323: INFO/MainProcess] mingle: searching for neighbors
[2020-02-23 18:31:42,377: INFO/MainProcess] mingle: all alone
[2020-02-23 18:31:42,431: INFO/MainProcess] celery@felixdeMacBook-Pro.local ready.
[2020-02-23 18:32:53,110: ERROR/MainProcess] Received unregistered task of type 'app.tasks.send_async_email'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you're using relative imports?

Please see
http://docs.celeryq.org/en/latest/internals/protocol.html
for more information.

The full contents of the message body was:
b'[[10, 20], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]' (83b)
Traceback (most recent call last):
  File "/Users/felixweek/venvs/py3_venv/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 559, in on_task_received
    strategy = strategies[type_]
KeyError: 'app.tasks.send_async_email'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶花眉 2022-09-19 01:10:13

问题解决了,我使用的工厂模式,要在create_app前导入执行函数,添加这一句
celery.conf['imports'] = ['app.tasks', ]

CELERY_BROKER_URL = "redis://:*****@localhost:6379/0"  
celery = Celery(__name_, broker=CELERY_BROKER_URL)  
# 在此处导入执行函数
celery.conf['imports'] = ['app.tasks', ]  
  
  
def create_app(env=None):  
    app = Flask(__name__)  
    app.config.from_object(config\[env\])  
  
    register_logging(app)  
    register_sentry(app)  
    register_routes(app)  
  
    register_command(app)  
  
    register_celery(app)  
  
    return app
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文