如何解决gunicorn 线程没有任何区别
以下是我加载 python 应用程序时的 linux top 输出
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,19 sleeping,0 zombie
%cpu(s): 5.2 us,1.6 sy,92.4 id,0.7 si,493.4 free,12262.1 used,3133.6 buff/cache
MiB Swap: 32768.0 total,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 zombie
%cpu(s): 26.2 us,9.9 sy,62.7 id,1.1 si,464.4 free,12291.4 used,3133.3 buff/cache
MiB Swap: 32768.0 total,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,27 sleeping,0 zombie
%cpu(s): 25.0 us,9.3 sy,64.8 id,0.9 si,441.8 free,12304.2 used,3143.0 buff/cache
MiB Swap: 32768.0 total,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
问题是,即使我通过了 --threads 5 ,我也看不到线程数有任何差异。我做错了什么。或者是我们无法使用 top 跟踪 gunicorn 线程。
解决方法
看起来如果我们使用 gthread 以外的不同工作类,gunicorn 会忽略线程标志。
这些是启动日志
[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
这个 thread 也是一样的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。