普通 uWSGI 错误:uwsgi_response_write_body_do() TIMEOUT

ada*_*czi 4 timeout requests uwsgi

这看起来像是 Nginx+uWSGI 超时的一个众所周知的问题,但我的设置不同,我找不到有关它的信息。

我在 uWSGI 上运行 Flask 应用程序,使用 http 协议(不是套接字)。当我查询 API 时,有时会收到错误消息:

uwsgi_response_write_body_do() TIMEOUT !!!
IOError: : write error
Run Code Online (Sandbox Code Playgroud)

我在“工作时间”收到它的频率更高,即在晚上不那么频繁。我正在查询长 JSON 数据(地理坐标,最多 50 MB),有时我根本没有得到响应,有时我得到了部分响应。这些是来自 Pythonrequests请求的错误:

requests.exceptions.ConnectionError: ('Connection aborted.', error(104, 'Connection reset by peer'))

requests.exceptions.ChunkedEncodingError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))

ValueError: Expecting object: line 1 column 5590371 (char 5590370)

ValueError: Unterminated string starting at: line 1 column 20998536 (char 20998535)

ValueError: end is out of bounds

# when I set `timeout=None` in Python
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='mydomain.com', port=80): Read timed out. (read timeout=None)
Run Code Online (Sandbox Code Playgroud)

连接通过普通 HTTP(无 S)。我的 uWSGI 配置如下:

[uwsgi]
module = run:app
pidfile = /tmp/app.pid
logto = /var/log/uwsgi/%n.log

master = true
processes = 2

http-socket = 0.0.0.0:someport
http-timeout = 300

die-on-term = true
Run Code Online (Sandbox Code Playgroud)

我使用uwsgi /path/to/inifile. 没有 Nginx,但实际上在使用 Nginx 之前,我遇到了同样的问题。所以我排除了这个。

如何避免这些超时?

小智 5

我也是用nginx + uwsgi 生成大文件,遇到同样的问题。我通过在 nginx use 指令中禁用 uwsgi 临时文件缓冲区解决了这个问题uwsgi_max_temp_file_size 0;

我认为从nginx到客户端的传输速度比从uwsgi到nginx的速度要慢得多。并且 nginx 的缓冲区已满,因此 uwsgi 等待很长时间才能有空间让 nginx 接收新数据,这会导致 uwsgi 的写入请求超时。

我在我的博客中发布了一个博客来分析这个问题。