How to Put a Rate Limit on a Celery Queue?

I read this in the celery documentation for Task.rate_limit:

Note that this is a per worker instance rate limit, and not a global rate limit. To enforce a global rate limit (e.g., for an API with a maximum number of requests per second), you must restrict to a given queue.

How do I put a rate limit on a celery queue?

2

3 Answers

Turns out it cant be done at queue level for multiple workers. IT can be done at queue level for 1 worker. Or at queue level for each worker.

So if u say 10 jobs/ minute on 5 workers. Your workers will process upto 50 jobs per minute collectively.

So to have only 10 jobs running at a time you either chose one worker. Or chose 5 workers with a limit of 2/minute.

Update: How to exactly put the limit in settings/configuration:

task_annotations = {'tasks.<task_name>': {'rate_limit': '10/m'}}

or change the same for all tasks:

task_annotations = {'*': {'rate_limit': '10/m'}}

10/m means 10 tasks per minute, /s would mean per second. More details here: Task annotations setting

7

hey I am trying to find a way to do rate limit on queue, and I find out Celery can't do that, however Celery can control the rate per tasks, see this:

so for a workaround, maybe you can set up one tasks per queue(which makes sense in a lot of situations), and put the limit on task.

1

You can set this limit in the flower > worker pane. there is a specified blank space for entering your limit there. The format that is suggested to be used is also like the below:

The rate limits can be specified in seconds, minutes or hours by appending “/s”, >“/m” or “/h” to the value. Tasks will be evenly distributed over the specified >time frame.

Example: “100/m” (hundred tasks a minute). This will enforce a minimum delay of >600ms between starting two tasks on the same worker instance.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest