2017-05-02 7 views
-1

タスク自体は正常に動作します(非同期で実行します)。しかし、celery beatを使用しようとすると動作しません。Celery-beat:なぜDjangoで動作しないのですか?

私は、http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#beat-custom-schedulersに続いた。

これは私のdjangoプロジェクト構造である:

. 
. 
├── clien 
│   ├── __init__.py 
│   ├── admin.py 
│   ├── management 
│   │   ├── __init__.py 
│   │   └── commands 
│   │    ├── __init__.py 
│   │    └── crawl_clien.py 
│   ├── migrations 
│   ├── models.py 
│   ├── tasks 
│   │   ├── __init__.py   ## ==> code 
│   │   └── crawl_clien_task.py ## ==> code 
│   ├── templates 
│   ├── urls.py 
│   └── views 
├── config 
│   ├── __init__.py  ## ==> code 
│   ├── celery.py  ## ==> code 
│   ├── settings 
│   │   ├── __init__.py 
│   │   ├── partials 
│   │   │   ├── __init__.py 
│   │   │   ├── base.py 
│   ├── urls.py 
│   └── wsgi.py 
├── manage.py 
. 
. 

はのみclienappに登録されています。ここで、コードは次のとおりです。

のconfig/celery.py

from __future__ import absolute_import, unicode_literals 
import os 
from celery import Celery 
from celery.schedules import crontab 

app = Celery('chois_crawler') 
app.config_from_object('django.conf:settings', namespace='CELERY') 
app.autodiscover_tasks() 
app.conf.beat_schedule = { 
    'my_task': { 
     'task': 'tasks.crawl_clien_task', 
     'schedule': crontab(minute='*/1'), 
    }, 
} 

のconfig/__ init__.py

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

CLIEN /タスク/ crawl_clien_task.py

from __future__ import absolute_import, unicode_literals 
from celery import shared_task, Celery 
from clien.management.commands import crawl_clien 

@shared_task 
def crawl_clien_task(): 
    print("hi") 

CLIEN /タスク/ __それはエラーを示し

from .crawl_clien_task import * 

それは動作しません... init__.py:

[2017-05-02 09:58:00,027: ERROR/MainProcess] Received unregistered task of type 'tasks.crawl_clien_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: 
b'[[], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]' (77b) 
Traceback (most recent call last): 
    File "/Users/Chois/.pyenv/versions/3.5.1/envs/chois_crawler/lib/python3.5/site-packages/celery/worker/consumer/consumer.py", line 559, in on_task_received 
    strategy = strategies[type_] 
KeyError: 'tasks.crawl_clien_task' 

は、だから私は他の方法を選択します。

config/celery.py

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

app = Celery('chois_crawler') 
app.config_from_object('django.conf:settings', namespace='CELERY') 
app.autodiscover_tasks() 

タスク/ crawl_clien_task.py

from __future__ import absolute_import, unicode_literals 
from celery import shared_task 
from celery.schedules import crontab 
from config.celery import app 

@app.on_after_configure.connect 
def setup_periodic_tasks(sender, **kwargs): 
    sender.add_periodic_task(
     crontab(minute='*/5'), 
     crawl_clien_task(), 
     name="hi", 
    ) 

@shared_task 
def crawl_clien_task(): 
    print("hi") 

しかし、それはどちらか、動作しません!

どうしたのですか?

+0

再起動の労働者としてのタスクが含まれ、その後、あなたはこのエラーを取得することはできませんビートする必要があります。 –

+0

@UsmanMaqbool両方の方法で再起動しましたが、まだ動作しませんでした – user3595632

+0

ファイルを設定する際にタスクを追加しましたか? –

答えて

0

あなたは

'appname.tasks.crawl_clien_task', 

ので

app.conf.beat_schedule = { 
    'my_task': { 
     'task': 'clien.tasks.crawl_clien_task', 
     'schedule': crontab(minute='*/1'), 
    }, 
} 
+0

残念ながら 'KeyError: 'clien.tasks.crawl_clien_task'' ....... – user3595632

+0

' clien'は 'INSTALLED_APP'に登録されていますが' config'は登録されていませんか? – user3595632

+0

このリンクをチェックするhttp://stackoverflow.com/questions/9769496/celery-received-unregistered-task-of-type-run-example – itzMEonTV

関連する問題