Python Gunicorn一次响应的请求不超过6个 给你一些背景:

Python Gunicorn一次响应的请求不超过6个 给你一些背景:,python,concurrency,kubernetes,gunicorn,Python,Concurrency,Kubernetes,Gunicorn,我有两个服务器环境运行同一个应用程序。第一个,我打算放弃,是一个标准的谷歌应用程序引擎环境,它有很多限制。第二个是Google Kubernetes集群,它使用Gunicorn运行我的Python应用程序 并发性 在第一台服务器上,我可以向应用程序发送多个请求,它将同时响应多个请求。我在两个环境中对应用程序同时运行了两批请求。在谷歌应用程序引擎上,第一批和第二批同时响应,第一批不会阻止第二批 在Kubernetes,服务器只同时响应6个,第一批阻塞第二批。我读过一些关于如何使用gevent或多线

我有两个服务器环境运行同一个应用程序。第一个,我打算放弃,是一个标准的谷歌应用程序引擎环境,它有很多限制。第二个是Google Kubernetes集群,它使用Gunicorn运行我的Python应用程序

并发性 在第一台服务器上,我可以向应用程序发送多个请求,它将同时响应多个请求。我在两个环境中对应用程序同时运行了两批请求。在谷歌应用程序引擎上,第一批和第二批同时响应,第一批不会阻止第二批

在Kubernetes,服务器只同时响应6个,第一批阻塞第二批。我读过一些关于如何使用gevent或多线程实现Gunicorn并发性的文章,他们都说我需要CPU内核,但问题是,无论我投入多少CPU,限制仍然存在。我尝试了从1VCPU到8VCPU的Google节点,但变化不大

你们能告诉我我可能遗漏了什么吗?也许谷歌集群节点有限

库伯内特斯反应瀑布 正如您所注意到的,第二批仅在第一批开始完成后才开始响应

应用程序引擎响应瀑布

您所描述的似乎是一个指示器,表明您正在运行Gunicorn服务器,并且该类服务于I/O绑定的应用程序。你能分享你的Gunicorn配置吗

谷歌的平台是否可能有某种自动缩放功能(我对他们的服务不太熟悉),而你的Kubernetes配置却没有

一般来说,增加单个实例的核心数只有在您还增加为处理传入请求而产生的工作线程数时才会有所帮助。请参阅,特别强调工作类型部分(以及为什么
sync
worker对于I/O绑定的应用程序不太理想)-这是一本很好的读物,并提供了有关此问题的更详细的解释

为了好玩,这里有一个小练习来比较这两种方法:

import time

def app(env, start_response):
    time.sleep(1) # takes 1 second to process the request
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return [b'Hello World']

使用4个同步工作进程运行Gunicorn:
Gunicorn--bind'127.0.0.1:9001'--工作进程4--工作进程类同步--chdir应用程序:应用程序

让我们同时触发8个请求:
ab-n8-c8“http://localhost:9001/“

实际上,我们将应用程序的吞吐量提高了一倍—只需~1秒即可回复所有请求

为了了解发生了什么,Gevent对其体系结构进行了详细介绍,并对co例程进行了更深入的解释


如果与您的问题的实际原因相去甚远,我提前表示歉意(我确实认为,您最初的评论中缺少一些其他信息,任何人都无法得出结论性的答案)。如果不是对你,我希望这会对其他人有所帮助。:)


还要注意,我把事情简单化了很多(我的例子是一个简单的概念证明),调整HTTP服务器配置主要是一项试错练习-这完全取决于应用程序的工作负载类型及其所处的硬件。

关于设置的问题-在这两种情况下,所有请求都针对同一域执行?是的!这可能是相关的:如果您愿意,您可以共享请求和响应头。我有一种感觉,我知道会发生什么。如果问题出在Chrome上,为什么它会在AppEngine上工作?我猜一个是HTTP 1.1,另一个是HTTP 2.0,这将改变连接的方式。如果您可以公布标题,它可能会有帮助:)请参见此处:
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:        gunicorn/19.8.1
Server Hostname:        localhost
Server Port:            9001

Document Path:          /
Document Length:        11 bytes

Concurrency Level:      8
Time taken for tests:   2.007 seconds
Complete requests:      8
Failed requests:        0
Total transferred:      1096 bytes
HTML transferred:       88 bytes
Requests per second:    3.99 [#/sec] (mean)
Time per request:       2006.938 [ms] (mean)
Time per request:       250.867 [ms] (mean, across all concurrent requests)
Transfer rate:          0.53 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.2      1       1
Processing:  1003 1504 535.7   2005    2005
Waiting:     1002 1504 535.8   2005    2005
Total:       1003 1505 535.8   2006    2006

Percentage of the requests served within a certain time (ms)
  50%   2006
  66%   2006
  75%   2006
  80%   2006
  90%   2006
  95%   2006
  98%   2006
  99%   2006
 100%   2006 (longest request)
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:        gunicorn/19.8.1
Server Hostname:        localhost
Server Port:            9001

Document Path:          /
Document Length:        11 bytes

Concurrency Level:      8
Time taken for tests:   1.005 seconds
Complete requests:      8
Failed requests:        0
Total transferred:      1096 bytes
HTML transferred:       88 bytes
Requests per second:    7.96 [#/sec] (mean)
Time per request:       1005.463 [ms] (mean)
Time per request:       125.683 [ms] (mean, across all concurrent requests)
Transfer rate:          1.06 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.4      1       2
Processing:  1002 1003   0.6   1003    1004
Waiting:     1001 1003   0.9   1003    1004
Total:       1002 1004   0.9   1004    1005

Percentage of the requests served within a certain time (ms)
  50%   1004
  66%   1005
  75%   1005
  80%   1005
  90%   1005
  95%   1005
  98%   1005
  99%   1005
 100%   1005 (longest request)