使用celery和redis遇到问题

 '__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式的配置, 应该没有问题吧?
阅读 10.2k
1 个回答

没有问题,能否看一下你的celery配置文件,错误显示key的问题

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏