【问题标题】:Celery periodic task doesn't start芹菜周期任务不启动
【发布时间】:2016-08-18 05:54:32
【问题描述】:

我在我的 django 项目中使用 celery 来运行定期任务。遵循 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")

当我运行 celery worker 时,我可以看到我的任务正在被发现 -

$ 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}>

我在这里错过了什么?

【问题讨论】:

  • 即使在运行 worker 并一起执行任务时,也不会执行计划任务 - python manage.py celery -A project worker -B --loglevel=DEBUG --app=celery_app:app

标签: python django celery


【解决方案1】:

尝试设置CELERY_ALWAYS_EAGER = False

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

查看文档here

【讨论】:

  • CELERY_ALWAYS_EAGER = False 没有任何区别。
  • 你的机器上运行 celery beat 吗?需要运行 Celery beat 才能执行周期性任务。
  • 如果您在原始问题的底部看到,我正在运行两者。工人再打。两者都通过manage.py。或者还有什么要运行的?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2018-03-11
    • 2018-04-05
    • 2013-01-12
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多