一次返回几十万行 JSON 会怎样?内存要多少?换成 orjson 能快多少?压缩划不划算?超过 gunicorn 超时会发生什么?这篇逐项量出来。
行数 耗时(ms) 响应(MB) 峰值内存(MB)
1000 1.1 0.10 0.2
10000 9.6 1.07 2.2
100000 94.7 11.14 22.3上一篇:Pydantic 在 Flask 中的使用;下一篇:Streaming JSON。
json.dumps(默认,带空格分隔) 72.6 ms 12.44 MB json.dumps(紧凑 separators) 73.2 ms 11.14 MB orjson.dumps 9.5 ms 11.14 MB
debug=False compact=None 响应 103 KB debug=True compact=None 响应 164 KB
gzip level 1: 11.14 MB → 1.17 MB(10.5%),压缩耗时 21 ms gzip level 6: 11.14 MB → 1.17 MB(10.5%),压缩耗时 50 ms gzip level 9: 11.14 MB → 1.14 MB(10.2%),压缩耗时 104 ms
这里用 Python 的 gzip 模块测压缩比与耗时;反向代理(nginx)压缩见部署部分。
/big(10 万行,8.0 MB):收到响应头用时 119 ms,读完全部用时 124 ms,Content-Length 头 = 7992597 /slow(睡 4 秒,gunicorn --timeout 2):客户端结果 HTTP 500,用时 2931 ms 服务端日志: ['[2026-10-02 13:14:54 +0800] [97468] [INFO] Booting worker with pid: 97468', '[2026-10-02 13:14:57 +0800] [97458] [CRITICAL] WORKER TIMEOUT (pid:97468)', '[2026-10-02 13:14:57 +0800] [97494] [INFO] Booting worker with pid: 97494']