Celery Batches is an experimental task class that buffers messages and processes them as a list within Celery workers. Task requests are held in memory until either a configured flush count or flush interval is reached. This library is actively maintained, currently at version 0.11, and provides continuous support for recent Python and Celery versions.
pip install celery-batchesVerified import paths — ran on the pinned version, not inferred.
This example defines a Celery task that uses `celery-batches` to process messages in groups of 10 or every 5 seconds. Individual tasks are sent using `.delay()`, and the `process_batch_task` receives a list of `SimpleRequest` objects. Remember to configure your Celery app and run a worker with `prefetch-multiplier=0` for `celery-batches` to operate correctly.
Consult the `celery-batches` GitHub README or PyPI page for the supported Celery and Python versions for your desired `celery-batches` version. Upgrade/downgrade `celery-batches`, Celery, or Python as needed.
Configure your Celery worker to use `--prefetch-multiplier=0` (or a value > `flush_every`). Be mindful of memory usage in production environments with `prefetch-multiplier=0`.
Inside your batched task, iterate through `requests` and call `app.backend.mark_as_done(request.id, individual_result, request=request)` for each `SimpleRequest` object to store individual results.
Upgrade to `celery-batches` version `0.10` or newer to resolve the bug where `flush_interval` could cause tasks to be stuck in the buffer.
For Celery versions 4.0 and higher, `from celery_batches import Batches`. If using Celery < 4.0, you would use `from celery.contrib.batches import Batches`.
Ensure your Celery worker is started with `--prefetch-multiplier=0` (or a value greater than your task's `flush_every`). Review `flush_every` (number of tasks) and `flush_interval` (time in seconds) on your `@app.task(base=Batches, ...)` decorator to match your desired batching behavior.
Access positional arguments via `request.args` (e.g., `request.args[0]`) and keyword arguments via `request.kwargs` (e.g., `request.kwargs['key']`).