2017-04-22 8 views
0

セロリのワーカープロセスの実行に問題があります。 私の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() 

私はこのエラー

[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' 

何が問題になっていますを取得する挿入されている

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

と新しいコマンドを使用して作業員を実行すると?これは私のmain()処理のためですか?どのようにこれを解決するための任意のアイデア?

+0

あなたのファイルの名前は何ですか? – masnun

+0

test.pyはこのコードを含む私のpythonファイルの名前です。 – kreishna

+0

実行してみてください: 'Celery worker -A test -l info -c 5'。ところで、なぜ主要な機能の中にあなたのコードをラッピングしていますか? – masnun

答えて

0

作業者は、定義されたモジュールからタスク関数をインポートできる必要があります。これは、別のスコープ(この場合はmain関数内)にネストされているときは不可能です。

メインスコープであなたのタスクを定義してみてください。

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

    def main(): 
     ... 
+0

このコードを使用すると、 'celery worker -A test --broker = redis://127.0.0.1:6379 --loglevel = info'を使用してワーカーを実行しようとしました。' File '/ home/csgo/server_manager/test.py "、行20、 @ celery.task NameError:name 'celery' is not definedセロリはメインの内部に定義されているため(上​​記参照)、任意のアイデアを修正する方法は? – kreishna

+0

'celery = Celery(__ name__、broker = 'redis:// localhost:6379/0')'のように、初期化を外に移動するだけです。関数内に 'celery.conf.update(app.config)'を置くことができます。 –

関連する問題