3

I have an AWS beanstalk env and have old setting of wsgi (given below), I do not have idea how does this work internally, can anybody guide me?

NumProcesses:7 -- number of process
NumThreads:5 -- number of thread in each process

How memory and cpu are being used with this configuration because there is no memory and cpu settings in AWS beanstalk level.

ankitbeohar90
  • 109
  • 13

2 Answers2

5

To add on to @Marcin

  1. Amazon linux 2 uses gunicorn
  2. workers are processes in gunicorn

Gunicorn should only need 4-12 worker processes to handle hundreds or thousands of requests per second.

Gunicorn relies on the operating system to provide all of the load balancing when handling requests. Generally, we (gunicorn creators) recommend (2 x $num_cores) + 1 as the number of workers to start off with. While not overly scientific, the formula is based on the assumption that for a given core, one worker will be reading or writing from the socket while the other worker is processing a request.

To see how the settings in the option settings map to gunicorn you can ssh into your eb instance, go

$ eb ssh
$ cd cd /var/app/current/
$ cat Procfile 
web: gunicorn --bind 127.0.0.1:8000 --workers=3 --threads=20 api.wsgi:application 

--threads A positive integer generally in the 2-4 x $(NUM_CORES) range. You’ll want to vary this a bit to find the best for your particular application’s work load.

The threads option only applies to gthread worker type. gunicons default worker class is sync, If you try to use the sync worker type and set the threads setting to more than 1, the gthread worker type will be used instead automatically

based on all the above I would personally choose

workers = (2 x $NUM_CORES ) + 1
threads = 4 x $NUM_CORES 

for a t3.medum instance that has 2 cores that translates to

workers = 5
threads = 8

obviously, you need to tweak this for your use case, and treat these as defaults that could very well not be right for your particular application use case, read the refs below to see how to choose the right setup for you use case

References:

REF: Gunicorn Workers and Threads REF: https://medium.com/building-the-system/gunicorn-3-means-of-concurrency-efbb547674b7 REF: https://docs.gunicorn.org/en/stable/settings.html#worker-class

Dr Manhattan
  • 13,537
  • 6
  • 45
  • 41
4

These parameters are part of configuration option for Python environment:

They mean (from docs):

  • NumProcesses: The number of daemon processes that should be started for the process group when running WSGI applications (default value 1).

  • NumThreads: The number of threads to be created to handle requests in each daemon process within the process group when running WSGI applications (default value 15).

Internally, these values map to uwsgi or gunicorn configuration options in your EB environment. For example:

uwsgi --http :8000 --wsgi-file application.py --master --processes 4 --threads 2

Their impact on memory and cpu usage of your instance(s) is based on your application and how resource intensive it is. If you are not sure how to set them up, maybe keeping them at default values would be a good start.

The settings are also available in the EB console, under Software category:

enter image description here

Marcin
  • 215,873
  • 14
  • 235
  • 294