我正在使用 Celery 5.3.4 运行 Django 应用程序。我使用 Redis 作为代理。
我想配置
task_time_limit
和task_time_soft_limit
选项,但在本地运行时无法让它们生效。我尝试将它们设置得很低,并运行一个缓慢的任务,sleep
10 秒:
app.conf.task_time_limit = 5
app.conf.task_soft_time_limit = 1
但是,当在本地运行 Celery Worker 时,它会完成缓慢的任务并且不会超时:
2024-03-05T15:46:04.647630Z [info ] Task slow_task[97f51f79-ac48-4aa3-9098-a3ec5a299adc] received [celery.worker.strategy]
2024-03-05T15:46:14.741217Z [info ] Task slow_task[97f51f79-ac48-4aa3-9098-a3ec5a299adc] succeeded in 10.093100208323449s: None [celery.app.trace]
我尝试过以多种不同的方式配置 Celery。这是我当前的配置,分布在 2 个文件中:
celery.py
:
import os
from celery import Celery
from celery.signals import worker_init
from django.conf import settings
# Set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kosa_api.settings")
app = Celery("kosa_api")
# 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 apps.
app.autodiscover_tasks()
app.conf.task_time_limit = 5
app.conf.task_soft_time_limit = 1
settings/celery.py
(这是在我的应用程序设置中导入的):
from kosa_api.settings.utils import get_safe_redis_url
CELERY_WORKER_HIJACK_ROOT_LOGGER = False
REDIS_URL = get_safe_redis_url()
CELERY_BROKER_URL = f"{REDIS_URL}/0"
CELERY_RESULT_BACKEND = f"{REDIS_URL}/2"
# only store results for a while to prevent redis from filling up
CELERY_RESULT_EXPIRES = timedelta(minutes=30)
# in tests, run celery tasks as regular functions
CELERY_TASK_ALWAYS_EAGER = TEST
CELERY_TASK_ACKS_LATE = True
# i tried all of the following, but none took effect:
# CELERY_CELERYD_SOFT_TIME_LIMIT = 1
# CELERY_CELERYD_TIME_LIMIT = 5
# CELERY_TASK_SOFT_TIME_LIMIT = 1
# CELERY_TASK_TIME_LIMIT = 5
我做错了什么?
使用方法如下。
app.conf.update(
task_soft_time_limit=1,
task_time_limit=5,
)
或者
CELERY_TASK_TIME_LIMIT = 5
CELERY_TASK_SOFT_TIME_LIMIT = 1