You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署Flask应用到App Engine标准环境遇请求超时(错误码123)

Flask应用部署至App Engine标准环境超时问题

我把Flask应用部署到App Engine标准环境,本地运行正常,部署也能成功,但访问URL时页面加载失败,日志里出现如下错误:

App Engine: Process terminated because the request deadline was exceeded. Please ensure that your HTTP server is listening for requests on 0.0.0.0 and on the port defined by the PORT environment variable. (Error code 123)

我用的app.run代码是:

if __name__ == '__main__':
    app.run(host='0.0.0.0')

以下是超时前GCP日志探索器的完整日志:

2022-07-26 11:47:00.542 BST
App Initialising

2022-07-26 11:47:00.544 BST
 * Serving Flask app 'app' (lazy loading)

2022-07-26 11:47:00.544 BST
 * Environment: production

2022-07-26 11:47:00.544 BST
 WARNING: This is a development server. Do not use it in a production deployment.

2022-07-26 11:47:00.544 BST
 Use a production WSGI server instead.

2022-07-26 11:47:00.544 BST
 * Debug mode: off

2022-07-26 11:47:00.556 BST
 * Running on all addresses (0.0.0.0)

2022-07-26 11:47:00.556 BST
 WARNING: This is a development server. Do not use it in a production deployment.

2022-07-26 11:47:00.556 BST
 * Running on http://127.0.0.1:5000

2022-07-26 11:47:00.556 BST
 * Running on http://169.254.8.1:5000 (Press CTRL+C to quit)

2022-07-26 11:49:31.899 BST
[start] 2022/07/26 10:49:31.898979 Quitting on terminated signal

2022-07-26 11:49:31.899 BST
[start] 2022/07/26 10:49:31.899629 Start program failed: failed to detect app after start: ForAppStart(): [aborted, context canceled. subject:"app/valid" Timeout:30m0s, attempts:119910 aborted, context canceled. subject:"app/invalid" Timeout:30m0s, attempts:119886]

测试GET请求首页的日志:

2022-07-26 11:50:43.136 BST

GET 500 301.663 s Chrome 103.0.0.0 /
188.221.25.131 - - [26/Jul/2022:03:50:43 -0700] GET / HTTP/1.1 500 - - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36" "<<app url>>" ms=301663 cpu_ms=0 cpm_usd=0 loading_request=0 instance=00c61b117cc57f1885aa1e4f9a85f795df32ab91d3ed079eeaf27721ad7817ec20ebabd30ca9d4226dd67175724a624eba33cd2da0023671d18f app_engine_release=1.9.71 trace_id=2ef242690a4b068b13493764e6279eb2

最终超时错误:

2022-07-26 11:55:44.799 BST
Process terminated because the request deadline was exceeded. Please ensure that your HTTP server is listening for requests on 0.0.0.0 and on the port defined by the PORT environment variable. (Error code 123)

请问问题出在哪?或者有什么排查方向?


问题分析与解决方法

核心问题

  1. 使用Flask开发服务器:日志明确提示开发服务器不适合生产部署,App Engine标准环境无法正确识别并转发请求到这个服务器。
  2. 未使用PORT环境变量:代码未指定App Engine提供的PORT环境变量,而是用默认的5000端口,导致平台无法连接到应用服务。

解决步骤

  1. 安装生产级WSGI服务器:在requirements.txt中添加gunicorn,执行安装后用它替代Flask自带服务器。
  2. 修改启动配置:
    • 删除if __name__ == '__main__':这段开发环境启动代码
    • 在app.yaml中配置启动命令:
      entrypoint: gunicorn -b :$PORT app:app
      
      其中$PORT自动读取平台环境变量端口,app:app表示从app.py文件加载名为app的Flask实例。
  3. 验证监听地址:Gunicorn默认监听0.0.0.0,无需额外配置,上述命令已确保绑定到正确地址和端口。

额外排查点

  • 检查app.yaml:确认运行时配置(如runtime: python39)正确,无错误的环境变量或启动命令。
  • 排查初始化阻塞:查看应用初始化日志,确认App Initialising之后没有卡住或未捕获的异常。
  • 本地模拟生产环境:用gunicorn -b :8080 app:app启动应用,验证是否能正常响应请求。

内容的提问来源于stack exchange,提问作者2bon2b

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 02:15:37