Vikash Singh
Vikash Singh

Reputation: 14001

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?

Upvotes: 44

Views: 31360

Answers (3)

Vikash Singh
Vikash Singh

Reputation: 14001

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

Upvotes: 25

Shayan
Shayan

Reputation: 160

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.

Upvotes: -2

Marshall X
Marshall X

Reputation: 794

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:

http://docs.celeryproject.org/en/latest/userguide/workers.html#rate-limits

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.

Upvotes: 1

Related Questions