gunicorn threads not making any difference

3.5k views Asked by At

Below is the linux top output when I load a python app

pipenv run gunicorn --worker-class=uvicorn.workers.UvicornWorker readme.main:app --bind=0.0.0.0

a) No Load

top
Tasks:   5 total,   1 running,   4 sleeping,   0 stopped,   0 zombie
%Cpu(s):  6.0 us,  1.3 sy,  0.0 ni, 92.3 id,  0.0 wa,  0.0 hi,  0.4 si,  0.0 st
MiB Mem :  15889.1 total,    489.5 free,  12264.9 used,   3134.6 buff/cache
MiB Swap:  32768.0 total,  28738.1 free,   4029.9 used.   1518.2 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                                                                       
   17 root      20   0 3055008 471780 146180 S   0.3   2.9   0:16.26 gunicorn                                                                                                                                      
    1 root      20   0    5616   3332   2968 S   0.0   0.0   0:00.11 entrypoint.g.sh                                                                                                                               
   10 root      20   0   32004  25660   8228 S   0.0   0.2   0:01.61 gunicorn                                                                                                                                      
   33 root      20   0    5748   3580   3144 S   0.0   0.0   0:00.08 bash                                                                                                                                          
   39 root      20   0    9820   3448   2956 R   0.0   0.0   0:00.01 top                                                                                                                                           

top -H
Threads:  20 total,   1 running,  19 sleeping,   0 stopped,   0 zombie
%Cpu(s):  5.2 us,  1.6 sy,  0.0 ni, 92.4 id,  0.0 wa,  0.0 hi,  0.7 si,  0.0 st
MiB Mem :  15889.1 total,    493.4 free,  12262.1 used,   3133.6 buff/cache
MiB Swap:  32768.0 total,  28738.1 free,   4029.9 used.   1521.4 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                                                                       
   10 root      20   0   32004  25660   8228 S   0.3   0.2   0:01.62 gunicorn                                                                                                                                      
    1 root      20   0    5616   3332   2968 S   0.0   0.0   0:00.10 entrypoint.g.sh                                                                                                                               
   17 root      20   0 3055008 471780 146180 S   0.0   2.9   0:07.23 gunicorn                                                                                                                                      
   18 root      20   0 3055008 471780 146180 S   0.0   2.9   0:00.31 gunicorn                                                                                                                                      
   19 root      20   0 3055008 471780 146180 S   0.0   2.9   0:00.34 gunicorn                                                                                                                                      
   20 root      20   0 3055008 471780 146180 S   0.0   2.9   0:00.31 gunicorn                                                                                                                                      
   21 root      20   0 3055008 471780 146180 S   0.0   2.9   0:00.31 gunicorn                                                                                                                                      
   22 root      20   0 3055008 471780 146180 S   0.0   2.9   0:00.33 gunicorn                                                                                                                                      
   23 root      20   0 3055008 471780 146180 S   0.0   2.9   0:00.35 gunicorn                                                                                                                                      
   24 root      20   0 3055008 471780 146180 S   0.0   2.9   0:00.34 gunicorn                                                                                                                                      
   25 root      20   0 3055008 471780 146180 S   0.0   2.9   0:00.14 gunicorn                                                                                                                                      
   26 root      20   0 3055008 471780 146180 S   0.0   2.9   0:00.13 gunicorn                                                                                                                                      
   27 root      20   0 3055008 471780 146180 S   0.0   2.9   0:00.12 gunicorn                                                                                                                                      
   28 root      20   0 3055008 471780 146180 S   0.0   2.9   0:00.13 gunicorn                                                                                                                                      
   29 root      20   0 3055008 471780 146180 S   0.0   2.9   0:00.13 gunicorn                                                                                                                                      
   30 root      20   0 3055008 471780 146180 S   0.0   2.9   0:00.12 gunicorn                                                                                                                                      
   31 root      20   0 3055008 471780 146180 S   0.0   2.9   0:00.14 gunicorn                                                                                                                                      
   32 root      20   0 3055008 471780 146180 S   0.0   2.9   0:05.74 gunicorn                                                                                                                                      
   33 root      20   0    5748   3580   3144 S   0.0   0.0   0:00.07 bash                                                                                                                                          
   40 root      20   0    9820   3484   2992 R   0.0   0.0   0:00.01 top                                                                                                                                           


b)with some load

top
Tasks:   5 total,   2 running,   3 sleeping,   0 stopped,   0 zombie
%Cpu(s): 26.2 us,  9.9 sy,  0.0 ni, 62.7 id,  0.0 wa,  0.0 hi,  1.1 si,  0.0 st
MiB Mem :  15889.1 total,    464.4 free,  12291.4 used,   3133.3 buff/cache
MiB Swap:  32768.0 total,  28738.1 free,   4029.9 used.   1497.6 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                                                                       
   17 root      20   0 3718596 471780 146180 R 115.7   2.9   0:21.35 gunicorn                                                                                                                                      
    1 root      20   0    5616   3332   2968 S   0.0   0.0   0:00.11 entrypoint.g.sh                                                                                                                               
   10 root      20   0   32004  25660   8228 S   0.0   0.2   0:01.63 gunicorn                                                                                                                                      
   33 root      20   0    5748   3640   3144 S   0.0   0.0   0:00.08 bash                                                                                                                                          
   41 root      20   0    9820   3436   2944 R   0.0   0.0   0:00.01 top                                                                                                                                           

top -H
Threads:  29 total,   2 running,  27 sleeping,   0 stopped,   0 zombie
%Cpu(s): 25.0 us,  9.3 sy,  0.0 ni, 64.8 id,  0.0 wa,  0.0 hi,  0.9 si,  0.0 st
MiB Mem :  15889.1 total,    441.8 free,  12304.2 used,   3143.0 buff/cache
MiB Swap:  32768.0 total,  28738.1 free,   4029.9 used.   1475.9 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                                                                       
   17 root      20   0 3718596 471780 146180 R  82.7   2.9   0:23.56 gunicorn                                                                                                                                      
   46 root      20   0 3718596 471780 146180 S   3.7   2.9   0:00.60 gunicorn                                                                                                                                      
   50 root      20   0 3718596 471780 146180 S   3.7   2.9   0:00.60 gunicorn                                                                                                                                      
   32 root      20   0 3718596 471780 146180 S   3.3   2.9   0:06.35 gunicorn                                                                                                                                      
   42 root      20   0 3718596 471780 146180 S   3.3   2.9   0:00.60 gunicorn                                                                                                                                      
   43 root      20   0 3718596 471780 146180 S   3.3   2.9   0:00.61 gunicorn                                                                                                                                      
   45 root      20   0 3718596 471780 146180 S   3.3   2.9   0:00.60 gunicorn                                                                                                                                      
   47 root      20   0 3718596 471780 146180 S   3.3   2.9   0:00.59 gunicorn                                                                                                                                      
   44 root      20   0 3718596 471780 146180 S   3.0   2.9   0:00.60 gunicorn                                                                                                                                      
   48 root      20   0 3718596 471780 146180 S   3.0   2.9   0:00.60 gunicorn                                                                                                                                      
   49 root      20   0 3718596 471780 146180 S   3.0   2.9   0:00.60 gunicorn                                                                                                                                      
   51 root      20   0    9820   3528   3032 R   0.3   0.0   0:00.01 top                                                                                                                                           
    1 root      20   0    5616   3332   2968 S   0.0   0.0   0:00.10 entrypoint.g.sh                                                                                                                               
   10 root      20   0   32004  25660   8228 S   0.0   0.2   0:01.63 gunicorn                                                                                                                                      
   18 root      20   0 3718596 471780 146180 S   0.0   2.9   0:00.31 gunicorn                                                                                                                                      
   19 root      20   0 3718596 471780 146180 S   0.0   2.9   0:00.34 gunicorn                                                                                                                                      
   20 root      20   0 3718596 471780 146180 S   0.0   2.9   0:00.31 gunicorn                                                                                                                                      
   21 root      20   0 3718596 471780 146180 S   0.0   2.9   0:00.31 gunicorn                                                                                                                                      
   22 root      20   0 3718596 471780 146180 S   0.0   2.9   0:00.33 gunicorn                                                                                                                                      
   23 root      20   0 3718596 471780 146180 S   0.0   2.9   0:00.35 gunicorn                                                                                                                                      
   24 root      20   0 3718596 471780 146180 S   0.0   2.9   0:00.34 gunicorn                                                                                                                                      
   25 root      20   0 3718596 471780 146180 S   0.0   2.9   0:00.14 gunicorn                                                                                                                                      
   26 root      20   0 3718596 471780 146180 S   0.0   2.9   0:00.13 gunicorn                                                                                                                                      
   27 root      20   0 3718596 471780 146180 S   0.0   2.9   0:00.12 gunicorn                                                                                                                                      
   28 root      20   0 3718596 471780 146180 S   0.0   2.9   0:00.13 gunicorn                                                                                                                                      
   29 root      20   0 3718596 471780 146180 S   0.0   2.9   0:00.13 gunicorn                                                                                                                                      
   30 root      20   0 3718596 471780 146180 S   0.0   2.9   0:00.12 gunicorn                                                                                                                                      
   31 root      20   0 3718596 471780 146180 S   0.0   2.9   0:00.14 gunicorn                                                                                                                                      
   33 root      20   0    5748   3640   3144 S   0.0   0.0   0:00.07 bash                                                                                                                                          

The issue is even if I pass --threads 5 , I dont see any difference in the count of threads either.What am I doing wrong.Or is it that we cannot track the gunicorn threads using top.

1

There are 1 answers

0
crackerplace On

Looks like if we use a different worker class other than gthread, gunicorn ignores the threads flag.

These are the startup logs

[2021-04-17 14:05:52 +0000] [10] [INFO] Listening at: http://0.0.0.0:8000 (10)
[2021-04-17 14:05:52 +0000] [10] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2021-04-17 14:05:52 +0000] [17] [INFO] Booting worker with pid: 17


If I remove the worker class, the threads worker is being used.

[2021-04-17 14:10:55 +0000] [10] [INFO] Starting gunicorn 20.1.0
[2021-04-17 14:10:55 +0000] [10] [INFO] Listening at: http://0.0.0.0:8000 (10)
[2021-04-17 14:10:55 +0000] [10] [INFO] Using worker: threads
[2021-04-17 14:10:55 +0000] [17] [INFO] Booting worker with pid: 17

This thread is also states the same.