如何使用Django在Celery中设置每个孩子的最大任务?

时间:2018-06-12 15:16:22

标签: django celery

我正在尝试在我的Django设置中为Celery设置一些设置,但无论我把它放在哪里:

CELERYD_MAX_TASKS_PER_CHILD=1

它始终允许同时启动多个任务。我尝试将其放入settings.pyproj.settings。我的celery.py如下:

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

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

app = Celery('proj', backend='redis://', broker='redis://localhost')


# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

2 个答案:

答案 0 :(得分:0)

默认情况下没有限制。

尝试

from celery import conf
conf.CELERYD_MAX_TASKS_PER_CHILD = 1 #max_tasks_per_child

此外,您可以在启动时将cmd作为参数传递给它(取决于版本):

--maxtasksperchild=1 

--max-tasks-per-child=1

Source

答案 1 :(得分:0)

应该去的地方是settings.py

CELERY_WORKER_CONCURRENCY = 1   # this is the one I was actually looking for
CELERY_MAX_TASKS_PER_CHILD = 1