簡體   English   中英

芹菜(燒瓶)正在運行的工人/名稱不正確

[英]celery (flask) running worker /incorrect name

我在運行芹菜工作者進程時遇到問題。 我的python代碼(test.py):

from flask import *
from celery import *


def main():
    #flask related code
    app = Flask(__name__)
    app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
    app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'

    celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)

    @celery.task
        def my_background_task(arg1, arg2):
            f = open('file.txt','w')
            f.write('answer:'+str(arg1))
            f.close()
            return 1

    @app.route('/createServer', methods=['POST'])
         def createServer():
             task = my_background_task.apply_async(args=["test0", "test1"], countdown=10)
             return jsonify({"status": "done"})

    app.run(host='127.0.0.1', port=8080, debug=True)

if __name__ == "__main__":
    main()

當我使用

celery worker -A test --broker=redis://127.0.0.1:6379  --loglevel=info

並插入一個新命令我得到這個錯誤

[2017-04-22 18:02:09,184: ERROR/MainProcess] Received unregistered task of type u'__init__.my_background_task'.
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:
'[["test0", "test1"], {}, {"chord": null, "callbacks": null, "errbacks": null, "chain": null}]' (93b)
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/celery/worker/consumer/consumer.py", line 559, in on_task_received
    strategy = strategies[type_]
KeyError: u'__init__.my_background_task'

怎么了? 看來這是因為我的main()處理? 任何想法如何解決這個問題?

工作人員需要能夠從定義任務的模塊中導入任務功能,而當它嵌套在另一個作用域中(在這種情況下是在main功能中)時,這是不可能的。

嘗試在主要范圍內定義任務:

    @celery.task
    def my_background_task(arg1, arg2):
        ...

    def main():
        ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM