使用celery和redis遇到问题

 Mrheartheart 发布于 2022-10-27 12:31
 '__main__.add'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you are using relative imports?
Please see http://bit.ly/gLye1c for more information.

The full contents of the message body was:
{'callbacks': None, 'eta': None, 'kwargs': {}, 'id': 'ad766baf-c8d5-46ec-934c-e73d07b57930', 'args': (45, 789), 'errbacks': None, 'utc': True, 'taskset': None, 'task': '__main__.add', 'retries': 0, 'timelimit': (None, None), 'chord': None, 'expires': None} (260b)
Traceback (most recent call last):
  File "/home/hawk/g/f3/lib/python3.3/site-packages/celery/worker/consumer.py", line 455, in on_task_received
    strategies[name](message, body,
KeyError: '__main__.add'

这里是运行文件:

from flask import Flask
from celerycfg import make_celery

app = Flask(__name__)
###############################################################
app.config.update(
    CELERY_BROKER_URL='redis://localhost:6379',
    CELERY_RESULT_BACKEND='redis://localhost:6379'
)
celery = make_celery(app)
@celery.task()
def add_together(a, b):
    return a + b

@celery.task()
def add(x, y):
    return (x + y) * 100    
###############################################################
@app.route('/')
def hello_world():
    return 'Hello World!' + str(add_together.delay(45, 789))

@app.route('/sum')
def sum():
    return 'sum-----' + str(add.delay(45, 789).get())

@app.route('/hello')
def hello():
    res = add.delay(456, 789)
    return "Result: %d " % res.wait()
###############################################################
if __name__ == '__main__':
    app.run()

这里是使用得到的结果:
http://127.0.0.1:5000/sum

======================================================

Traceback (most recent call last):
      File "/home/hawk/g/f3/lib/python3.3/site-packages/celery/worker/consumer.py", line 455, in on_task_received
        strategies[name](message, body,
    KeyError: 'app.fcelery.fcelery.add'
    [2015-01-06 12:41:07,485: ERROR/MainProcess] Received unregistered task of type 'app.fcelery.fcelery.add'.
    The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you are using relative imports?
Please see http://bit.ly/gLye1c for more information.

The full contents of the message body was:
{'kwargs': {}, 'id': '283c4ec5-043a-4cc6-b857-7eb1160feb25', 'utc': True, 'taskset': None, 'callbacks': None, 'chord': None, 'timelimit': (None, None), 'expires': None, 'retries': 0, 'task': 'app.fcelery.fcelery.add', 'eta': None, 'args': (5678, 890), 'errbacks': None} (272b)
Traceback (most recent call last):
  File "/home/hawk/g/f3/lib/python3.3/site-packages/celery/worker/consumer.py", line 455, in on_task_received
    strategies[name](message, body,
KeyError: 'app.fcelery.fcelery.add'
    sum-----ad766baf-c8d5-46ec-934c-e73d07b57930

book/app/models
        /blog()
        /media/controllers.py
              /models.py
              /....
        /fcelery/fcelery.py
                /tasks.py
                /__init__.py
        /templates  
    /run.py
这是一个flask app的目录, 里面有多个blueprint, 在蓝图的controllers.py里面使用celery, 也是同样的报错。。。
    [2015-01-06 12:53:30,961: ERROR/MainProcess] Received unregistered task of type 'app.fcelery.tasks.add'.
The message has been ignored and discarded.

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

Please see http://bit.ly/gLye1c for more information.

The full contents of the message body was:
{'expires': None, 'utc': True, 'id': '7138afa4-19ee-477d-9fc5-ab081ea94519', 'kwargs': {}, 'callbacks': None, 'taskset': None, 'eta': None, 'errbacks': None, 'task': 'app.fcelery.tasks.add', 'retries': 0, 'timelimit': (None, None), 'chord': None, 'args': (5678, 890)} (270b)
Traceback (most recent call last):
  File "/home/hawk/g/f3/lib/python3.3/site-packages/celery/worker/consumer.py", line 455, in on_task_received
    strategies[name](message, body,
KeyError: 'app.fcelery.tasks.add'

哪里出错了?

补充一下:
我看到官方的大体意思是celery使用flask-app的配置等等
而我是使用独立的celery,也就是说:

../fcelery/这里是独立的celery配置 , 和 flask app 无关
          /。。。

../app/blog/controllers.py
      在这里  from fcelery.tasks import add
        res = add.delay(5678, 890)
        return "posts list---- %d "  % res.get()                 

 没有使用官方的那种flask-extension式的配置, 应该没有问题吧?
1 个回答
  • 没有问题,能否看一下你的celery配置文件,错误显示key的问题

    2022-10-28 11:32 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有