芹菜周期性任务无法启动

时间:2016-04-25 00:25:14

标签: python django celery

我在django项目中使用celery来执行定期任务。按照芹菜网站的标准教程,这是我的项目结构 项目

|_ settings.py
|_ __init__.py
|_ celery_app.py (instead of celery.py)
|_ app
   |_ tasks.py

settings.py中的相关部分如下所示 -

CELERY_RESULT_BACKEND = "amqp"
CELERY_IMPORTS = ["app.tasks"]

CELERY_ALWAYS_EAGER = True    

CELERY_RESULT_BACKEND = 'djcelery.backends.database:DatabaseBackend'
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'

CELERY_DB_REUSE_MAX = 1

CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'America/New York'

CELERYBEAT_SCHEDULE = {
    'test_task': {
        'task': 'tasks.test_task',
        'schedule': timedelta(seconds=5),
        'args': (),
    },
}

celery_app.py看起来像这样 -

from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
app = Celery('project')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

tasks.py看起来像这样 -

from __future__ import absolute_import
from celery.utils.log import get_task_logger
from celery import task
from celery_app import app

logger = get_task_logger(__name__)

@app.task
def test_task():
    for i in range(0, 4):
        logger.debug("celery test task")

当我经营芹菜工人时,我可以看到我的任务被发现 -

$ python manage.py celery -A project worker --loglevel=DEBUG --app=celery_app:app

 -------------- celery@-MBP.home v3.1.19 (Cipater)
---- **** ----- 
--- * ***  * -- Darwin-15.4.0-x86_64-i386-64bit
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         project:0x10d5b9a10
- ** ---------- .> transport:   amqp://sgcelery1:**@localhost:5672/sgceleryhost
- ** ---------- .> results:     djcelery.backends.database:DatabaseBackend
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- 
--- ***** ----- [queues]
 -------------- .> celery           exchange=celery(direct) key=celery


[tasks]
  . celery.backend_cleanup
  . celery.chain
  . celery.chord
  . celery.chord_unlock
  . celery.chunks
  . celery.group
  . celery.map
  . celery.starmap
  . app.tasks.test_task

[2016-04-24 23:54:55,452: INFO/MainProcess] Connected to amqp://sgcelery1:**@127.0.0.1:5672/sgceleryhost
[2016-04-24 23:54:55,471: INFO/MainProcess] mingle: searching for neighbors
[2016-04-24 23:54:56,481: INFO/MainProcess] mingle: all alone
[2016-04-24 23:54:56,497: WARNING/MainProcess] celery@-MBP.home ready.

当我运行beat时,它会显示从settings.py中获取的任务,但它实际上从未运行过。

$ python manage.py celery -A project beat --app=celery_app:app --loglevel=DEBUG

[2016-04-24 23:55:04,059: DEBUG/MainProcess] Current schedule:
<ModelEntry: test_task tasks.test_task(*[], **{}) {4}>

我在这里缺少什么?

2 个答案:

答案 0 :(得分:0)

尝试设置CELERY_ALWAYS_EAGER = False

设置CELERY_ALWAYS_EAGER = True使任务同步运行而不使用芹菜。通过将其切换为False将使芹菜捡起并定期运行。当您不希望芹菜处理任务时,False开关用于开发模式。

查看文档here

答案 1 :(得分:0)

尝试使用命令:
celery -A project worker -B -E -Q beat --concurrency=1 -l INFO
this link 关于芹菜键和选项