使用celery和redis遇到問題
PHPz
PHPz 2017-04-22 08:59:28
0
1
1482
 '__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式的配置, 应该没有问题吧?
PHPz
PHPz

学习是最好的投资!

全部回覆(1)
PHPzhong

沒有問題,能否看一下你的celery設定文件,錯誤顯示key的問題

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板