2016-11-11 11 views
8

私はthe Celery Djangoのチュートリアルに従います。例題のタスク(add, mul)は私にとって完璧に機能します。 res = add.delay(1,2); res.get()を実行したときに正しい応答が得られます。セロリのいくつかのタスクが動作し、その他のものが登録されていません

しかし、別のタスクres = sayhello.delay('trex')を実行しようとすると、*** NotRegistered: u'pipeline.tasks.sayhello'が表示されます。

私がres = sayhello('trex')を実行すると、resと入力するだけで結果が得られます。しかし、このようにして、セロリを使わずにornidarly関数を実行します。

タスクは、私は、問題は、私はpipeline/views.pyからsayhelloタスクを実行することができないということです./manage shell

>>> res = sayhello.delay('trex') 
>>> res.get() 
u'Hello trex' 

シェルジャンゴにそれを実行している場合にのみに動作します。しかし、そこから作業addmulを実行できます。

なぜですか? views.pyからタスクを正しく実行するにはどうすればよいですか?

エラーメッセージ全体:

[2016-11-11 10:56:09,870: ERROR/MainProcess] Received unregistered task of type u'pipeline.tasks.sayhello'. 
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: 
'[["tiger"], {}, {"chord": null, "callbacks": null, "errbacks": null, "chain": null}]' (84b) 
Traceback (most recent call last): 
    File "/home/trex/Development/Sirius/new/rocket/rocket-venv/local/lib/python2.7/site-packages/celery/worker/consumer/consumer.py", line 549, in on_task_received 
    strategy = strategies[type_] 
KeyError: u'pipeline.tasks.sayhello' 

Djangoのバージョン

1.9.7 

セロリバージョン:

celery==4.0.0 
django-celery==3.1.17 

Djangoプロジェクトディレクトリツリー:

rocket 
├── etl 
│   ├── etl 
│   │   ├── celery.py 
│   │   ├── __init__.py 
│   │   ├── settings 
│   │   │   ├── base.py 
│   │   │   ├── dev.py 
│   │   │   ├── __init__.py 
│   │   │   ├── production.py 
│   │   │   └── test.py 
│   │   ├── urls.py 
│   │   ├── wsgi.py 
│   ├── manage.py 
│   ├── pipeline 
│   │   ├── __init__.py 
│   │   ├── models.py 
│   │   ├── tasks.py 
│   │   ├── tests.py 
│   │   ├── urls.py 
│   │   ├── views.py 

ETL /パイプライン/ views.py

from .tasks import * 

def get_response(request): 
    result = add.delay(1, 2) 
    result.get() 
    result = sayhello.delay('tiger') 
    result.get() 

ETL /パイプライン/ tasks.py

from __future__ import absolute_import, unicode_literals 
from celery import shared_task 

@shared_task 
def add(x, y): 
    return x + y 

@shared_task 
def mul(x, y): 
    return x * y 

@shared_task 
def sayhello(name): 
    return "Hello %s" % name 

はまた、私はこれを試してみました:

from celery.decorators import task 

@task(name="sayhello") 
def sayhello(name): 
    return "Hello {0}".format(name) 

ETL/celery.py

from __future__ import absolute_import, unicode_literals 
import os 
from celery import Celery 

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'etl.settings.base') 
app = Celery('etl') 
app.config_from_object('django.conf:settings', namespace='CELERY') 
app.autodiscover_tasks() 

@app.task(bind=True) 
def debug_task(self): 
    print('Request: {0!r}'.format(self.request)) 

ETL/__ init__py

from __future__ import absolute_import, unicode_literals 
from .celery import app as celery_app 
__all__ = ['celery_app'] 

ETL /設定/ base.py

... 
CELERY_BROKER_URL = 'redis://localhost:6379' 
CELERY_RESULT_BACKEND = 'redis://localhost:6379' 
CELERY_ACCEPT_CONTENT = ['application/json'] 
CELERY_TASK_SERIALIZER = 'json' 
CELERY_RESULT_SERIALIZER = 'json' 
CELERY_TIMEZONE = 'Europe/London' 
CELERY_IMPORTS = ('pipeline.tasks',) 

答えて

4

T彼のエラーはCELERY_IMPORTS設定があなたのetl/settings/base.pyファイルで正しく動作していないためです。 は、だから私の提案は次のとおりです。

CELERY_IMPORTS = ('pipeline.tasks' ,) 

からカンマを削除し、問題が解決しない場合は、このコマンドを実行します。

celery -A pipeline.tasks worker --loglevel=DEBUG 

もう一つは、あなたのtasks.pyファイルのニーズをimportするためには、settings.pyに登録されているDjangoアプリに入ってください。この点も確認してください。ありがとうございます。

+0

はい、カンマです。 – trex

+0

しかし、それは他のタスクではなぜ機能しますか?理解しないでください。 – trex

+0

最後にカンマなしで作業を停止しました。 'add'タスクはコンマでのみ動作します。タスク 'sayhello'はまだ動作しません。私は理解していない、それは動作し、それは動作を停止した。 – trex

関連する問題