为什么具有优先级的消息不显示在队列中?(芹菜)

2024-04-16 08:34:47 发布

您现在位置:Python中文网/ 问答频道 /正文

在Celery中,我将消息发布到具有指定优先级的队列,但它们不会显示在Redis后端的celery列表或flower monitoring中。你知道吗

我希望能够看到在任何给定的时间内队列中有多少条消息,这样我就可以看到是否需要更多的工作人员。你知道吗

我使用以下代码调用我的消息:

args = ("2eb89997-7e77-44ee-8bf5-077e5083b7e8", { "body": "hello-world 1", "json": { "time": 1 }, "source": "reddit", "link": "a", "username" : "a"},)
app.send_task("main.tasks.save_message_mid_priority", args=args, priority=1)

如果我调用没有优先级kwarg的消息,则显示在默认的celery队列中,我可以监视它们。如果我通过了,那我就不会


这是我的celery.app

from __future__ import absolute_import, unicode_literals
from django.conf import settings
import os
from celery import Celery
from kombu import Queue, Exchange

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

app = Celery('***', broker=settings.REDIS_URL)

# 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()

app.conf.broker_transport_options = {
    'priority_steps': list(range(4)),
}

app.conf.task_default_priority = 3
app.conf.task_queue_max_priority = 3

app.conf.task_soft_time_limit = 60
app.conf.task_acks_late = True
app.conf.worker_prefetch_multiplier = 1
app.conf.task_ignore_result = True

我的任务定义如下:

@app.task(bind=True, priority=0)
def save_message_high_priority(self, campaign_id, message):
    l.info("save_message: %s %s", campaign_id, self.priority)
    return save_message(campaign_id, message)

@app.task(bind=True, priority=1)
def save_message_mid_priority(self, campaign_id, message):
    l.info("save_message: %s %s", campaign_id, self.priority)
    return save_message(campaign_id, message)

@app.task(bind=True, priority=3)
def save_message_low_priority(self, campaign_id, message):
    l.info("save_message: %s %s", campaign_id, self.priority)
    return save_message(campaign_id, message)


Tags: fromimportselfidtrueapp消息message