Dynamic number of threads in celery

Mohamed Almadih

Mohamed Almadih

8/2/2022
1 min read
Dynamic number of threads in celery

I guess this a followup to the previous post, where i talked about celery and some pref and examples about it, i think this could be a series where i show the problems i faced during the project and how i overcome them.

The problem

The project is running in multiple servers with different amount of resources, the mistake i did is running celery with fixed number of threads in all servers this cause smaller servers to hang since the receive huge number of tasks larger thant their resources.

The solution

Solution is very simple i came up with an equation to calculate threads number based on amount of CPUs and Ram the server has.

1
#worker.sh
2
cpus=$(nproc --all)
3
ram=$(free -g | awk '/^Mem:/{print $2}')
4
threads=$((cpus*15 + ram*15))
5
6
celery -A celery_app.tasks worker --loglevel=info --pool=threads -c $threads

the 15 here is arbitrary number, of course this should be determined carefully based on tasks type and wight.

Related Posts

Convert sync code to async in python

Asynchronous programming has become increasingly popular in recent years, as it allows you to write code that can perform multiple tasks concurrently.

Download Videos from twitter using telegram bot

I normally don't like to download whole app just to do a simple thing, I tried to download a video from twitter...

Async Distributed tasks in python with Celery

I was working on a project where we need to collect data from domains, since...