gunicorn uvicorn worker.py如何遵守限制货币设置



FastAPI使用gunicorn启动uvicorn工人,如中所述https://www.uvicorn.org/settings/

然而,gunicorn不允许使用自定义设置启动uvicorn,如中所述https://github.com/encode/uvicorn/issues/343

建议重写源文件中的config_kwargs的问题如下https://github.com/encode/uvicorn/blob/master/uvicorn/workers.py

我们尝试过,但uvicorn不遵守来源:中多个uvicorn文件中的设置limit_concurrency

https://github.com/encode/uvicorn/blob/master/uvicorn/workers.py

# fail
config_kwargs = {
"app": None,
"log_config": None,
"timeout_keep_alive": self.cfg.keepalive,
"timeout_notify": self.timeout,
"callback_notify": self.callback_notify,
"limit_max_requests": self.max_requests, "limit_concurrency": 10000,
"forwarded_allow_ips": self.cfg.forwarded_allow_ips,
}

https://github.com/encode/uvicorn/blob/master/uvicorn/main.py

# fail
kwargs = {
"app": app,
"host": host,
"port": port,
"uds": uds,
"fd": fd,
"loop": loop,
"http": http,
"ws": ws,
"lifespan": lifespan,
"env_file": env_file,
"log_config": LOGGING_CONFIG if log_config is None else log_config,
"log_level": log_level,
"access_log": access_log,
"interface": interface,
"debug": debug,
"reload": reload,
"reload_dirs": reload_dirs if reload_dirs else None,
"workers": workers,
"proxy_headers": proxy_headers,
"forwarded_allow_ips": forwarded_allow_ips,
"root_path": root_path,
"limit_concurrency": 10000,
"backlog": backlog,
"limit_max_requests": limit_max_requests,
"timeout_keep_alive": timeout_keep_alive,
"ssl_keyfile": ssl_keyfile,
"ssl_certfile": ssl_certfile,
"ssl_version": ssl_version,
"ssl_cert_reqs": ssl_cert_reqs,
"ssl_ca_certs": ssl_ca_certs,
"ssl_ciphers": ssl_ciphers,
"headers": list([header.split(":") for header in headers]),
"use_colors": use_colors,
}

uvicorn怎么能被迫尊重这种环境?我们仍然从FastAPI 中得到503个错误

-------更新-----------gunicorn设置CCD_ 2在进行分配给许多工作者的100个并行请求时仍然引起503。

然而,我认为这是一个更复杂的问题:我们的API端点做了大量繁重的工作,通常需要5秒钟才能完成。

2芯、2名工人的压力测试:

  • A。100多个并发请求,端点负载过重--工作连接1
  • B。100多个并发请求,端点负载过重--工作连接1000
  • C。100多个并发请求,端点低负载--工作连接1
  • D。100多个并发请求,端点低负载--工作连接1000

实验A和B都产生了503个响应,因此假设工作连接设置确实有效,过多的模拟连接似乎不会导致我们的503错误。

我们对这种行为感到困惑,因为我们希望gunicorn/uvicorn对工作进行排队,而不会抛出503个错误。

来自gunicorn文档

worker-connections

同时运行的客户端的最大数量。

和来自uvicorn doc

limit-concurrency

在发出HTTP 503响应之前,允许的最大并发连接数或任务数。

根据此信息,两个设置变量都在做相同的事情。所以

uvicorn--limit-concurrency 100application:demo_app

几乎与相同

gunicorn--worker-connections 100-k uvicorn.workers.UvicornWorker application:demo_app

注意:我还没有对此进行任何真正的测试,如果我错了,请纠正我。


此外,您可以通过对uvicorn.workers.UvicornWorker类进行子类化来设置limit-concurrency(或limit_concurrency(

from uvicorn.workers import UvicornWorker

class CustomUvicornWorker(UvicornWorker):
CONFIG_KWARGS = {
"loop": "uvloop",
"http": "httptools",
"limit_concurrency": 100
}

现在使用这个CustomUvicornWorkergunicorn命令作为

gunicorn-k path.to.custom_worker.CustomUvicornWorkerapplication:demo_app

注意:您可以在CustomUvicornWorker类中检查self.config.limit_concurrency,以确保该值设置正确。

最新更新