Celery-beat:为什么它在Django中不起作用?

时间:2017-05-02 00:46:17

标签: django celery django-celery celerybeat

任务本身运行良好(异步运行),但是当我尝试使用celery beat时,它不起作用。

我跟着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
.
.

clien仅注册了app。以下是代码:

配置/ 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'),
    },
}

配置/ __初始化__。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 /任务/ __初始化__。PY

from .crawl_clien_task import *

它不起作用......

显示错误:

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

所以我选择另一种方式:

配置/ 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")

但它也不起作用!

它出了什么问题?

1 个答案:

答案 0 :(得分:0)

您应该将任务包含在

'appname.tasks.crawl_clien_task',

所以

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