Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the appropriate number of Gunicorn workers for each Amazon Instance Type?

Tags:

I'm currently trying to figure out what the appropriate number of workers is for each Amazon Instance Type. I used to run one Gunicorn worker, however that proved to be quite slow.

Many developers are currently using this formula to gauge how many workers would be suitable:

NUM_WORKERS=3  #recommended formula here is 1 + 2 * NUM_CORES 

The problem I'm having is that Amazon isn't quite clear as to the number of cores each instance is running. For example, an M1 Small Instance has 1 EC2 Compute Unit (1 virtual core with 1 EC2 Compute Unit)

What does that essentially mean? That it has one core? or that it has two cores?

like image 492
deadlock Avatar asked Apr 12 '13 19:04

deadlock


People also ask

How many requests can a Gunicorn worker handle?

Requests per second is not the same as "concurrent requests". If each request takes exactly 1 millisecond to handle, then a single worker can serve 1000 RPS.

How does Gunicorn handle multiple requests?

Gunicorn also allows for each of the workers to have multiple threads. In this case, the Python application is loaded once per worker, and each of the threads spawned by the same worker shares the same memory space. Gunicorn with threads setting, which uses the gthread worker class.

What does workers mean in Gunicorn?

Gunicorn is a pre-fork model server, which means that a master process is started and then a several worker processes are forked from the master. The worker processes are responsible for handling requests and returning a response to the client. Gunicorn Support multiple worker types: Sync Workers. Async Workers.


1 Answers

I know this is a old question. But I think I have a better answer to this question. Gunicorn docs suggests that 2n+1 [gunicorn -w <2n+1> myapp:wsgi] is a good guess for number of workers (Yes, n = number of cores). I came up with a tiny shell script to apply this formula. All you need to do is this:

gunicorn -w $(( 2 * `cat /proc/cpuinfo | grep 'core id' | wc -l` + 1 )) myapp:wsgi 

Where the command

cat /proc/cpuinfo | grep 'core id' | wc -l 

will return the total number of actual CPU cores (n). So

$(( 2 * `cat /proc/cpuinfo | grep 'core id' | wc -l` + 1 )) 

equates to 2n+1 formula.

This will apply 2n+1 formula to all the linux-based machines. You dont need to know the number of workers for each type of instance or anything like that.

Reference: http://dhilipsiva.com/2015/10/22/appropriate-number-of-gunicorn-workers.html

like image 148
dhilipsiva Avatar answered Sep 20 '22 13:09

dhilipsiva