Celery - 批量队列任务

Celery - bulk queue tasks

我有一些代码可以对大量(1000 多个)芹菜任务进行排队,例如,我们假设它是这样的:

for x in xrange(2000):
    example_task.delay(x)

是否有better/more一次将大量任务排队的有效方法?他们都有不同的论据。

调用大量任务对您的 celery worker 来说是不健康的。 此外,如果您正在考虑收集调用任务的结果,那么您的代码将不是最佳的。

您可以按特定大小分批处理任务。考虑下面提到的例子 link.

http://docs.celeryproject.org/en/latest/userguide/canvas.html#chunks

当我们想使用 Celery 处理几百万个 PDF 时,我们 运行 也陷入了这个问题。我们的解决方案是写一些我们称之为 CeleryThrottle 的东西。基本上,您使用所需的 Celery 队列和您想要的任务数量配置油门,然后在循环中创建任务。当您创建任务时,节流阀将监控实际队列的长度。如果它消耗得太快,它会在一段时间内加快你的循环,这样更多的任务就会被添加到队列中。如果队列变得太大,它会减慢你的循环并让一些任务完成。

代码如下:

class CeleryThrottle(object):
    """A class for throttling celery."""

    def __init__(self, min_items=100, queue_name='celery'):
        """Create a throttle to prevent celery run aways.

        :param min_items: The minimum number of items that should be enqueued. 
        A maximum of 2× this number may be created. This minimum value is not 
        guaranteed and so a number slightly higher than your max concurrency 
        should be used. Note that this number includes all tasks unless you use
        a specific queue for your processing.
        """
        self.min = min_items
        self.max = self.min * 2

        # Variables used to track the queue and wait-rate
        self.last_processed_count = 0
        self.count_to_do = self.max
        self.last_measurement = None
        self.first_run = True

        # Use a fixed-length queue to hold last N rates
        self.rates = deque(maxlen=15)
        self.avg_rate = self._calculate_avg()

        # For inspections
        self.queue_name = queue_name

    def _calculate_avg(self):
        return float(sum(self.rates)) / (len(self.rates) or 1)

    def _add_latest_rate(self):
        """Calculate the rate that the queue is processing items."""
        right_now = now()
        elapsed_seconds = (right_now - self.last_measurement).total_seconds()
        self.rates.append(self.last_processed_count / elapsed_seconds)
        self.last_measurement = right_now
        self.last_processed_count = 0
        self.avg_rate = self._calculate_avg()

    def maybe_wait(self):
        """Stall the calling function or let it proceed, depending on the queue.

        The idea here is to check the length of the queue as infrequently as 
        possible while keeping the number of items in the queue as closely 
        between self.min and self.max as possible.

        We do this by immediately enqueueing self.max items. After that, we 
        monitor the queue to determine how quickly it is processing items. Using 
        that rate we wait an appropriate amount of time or immediately press on.
        """
        self.last_processed_count += 1
        if self.count_to_do > 0:
            # Do not wait. Allow process to continue.
            if self.first_run:
                self.first_run = False
                self.last_measurement = now()
            self.count_to_do -= 1
            return

        self._add_latest_rate()
        task_count = get_queue_length(self.queue_name)
        if task_count > self.min:
            # Estimate how long the surplus will take to complete and wait that
            # long + 5% to ensure we're below self.min on next iteration.
            surplus_task_count = task_count - self.min
            wait_time = (surplus_task_count / self.avg_rate) * 1.05
            time.sleep(wait_time)

            # Assume we're below self.min due to waiting; max out the queue.
            if task_count < self.max:
                self.count_to_do = self.max - self.min
            return

        elif task_count <= self.min:
            # Add more items.
            self.count_to_do = self.max - task_count
            return

我们这样使用它:

throttle = CeleryThrottle(min_items=30, queue_name=queue)
for item in items:
    throttle.maybe_wait()
    do_something.delay()

所以它使用起来非常简单,而且它很好地使队列保持在一个愉快的位置——不太长,也不太短。它保持队列消耗速率的滚动平均值,并且可以相应地调整自己的计时器。