我在我的django项目中使用芹菜来运行周期性任务。按照芹菜网站上的标准教程,这是我的项目结构项目
|_ 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}>
我在这里错过了什么?
尝试设置 CELERY_ALWAYS_EAGER = False
。
设置CELERY_ALWAYS_EAGER = True
可使任务同步运行,而无需使用芹菜。通过将其切换到 False 将使芹菜节拍捡起它并定期运行它。False 开关在开发模式下用于不希望芹菜处理任务时。
在此处查看文档
尝试使用 command:
celery -A project worker -B -E -Q beat --concurrency=1 -l INFO
此链接 关于芹菜钥匙和选项