如何解决由 "This request caused a new process to be started for your application..." 引起的应用引擎中的高延迟?

How solve High latency in app engine caused by "This request caused a new process to be started for your application..."?

使用标准环境应用引擎、python 3.7 和云 sql (Mysql)

的应用

检查日志有一些延迟非常高(超过 4 秒),而预期是 800 毫秒。所有这些日志都伴随着这条消息:

"This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application."

我知道当它提到一个新进程时,它指的是一个新实例的部署(因为我使用自动缩放)但是奇怪的是,在某些情况下将这些日志与实例部署进行比较时它匹配,但在其他情况下不匹配。

我的问题是,如何减少这些延迟?

应用引擎配置为:

runtime: python37
env: standard
instance_class: F1
handlers:
  - url: /static/(.*)
    static_files: static/
    require_matching_file: false
    upload: static/.*
  - url: /.*
    script: auto
    secure: always
  - url: .*
    script: auto
automatic_scaling:
  min_idle_instances: automatic
  max_idle_instances: automatic
  min_pending_latency: automatic
  max_pending_latency: automatic
network: {}

正如您所注意到的,只要 App Engine 需要为您的应用程序启动一个新实例,就会发生这些较慢的请求,因为初始加载很慢(这些称为 "loading requests")。

但是,App Engine 确实提供了一种使用方法 "warmup" requests -- 基本上,向您的应用程序发出虚拟请求,以在实际需要实例之前启动实例。这可以减少但不会消除影响用户的加载请求。

这可能会稍微增加您的成本,但它应该会减少加载请求延迟,因为这些虚拟请求将消耗启动新实例的成本。

在python 3.7运行时,你可以在app.yaml中的inbound_services指令中添加一个"warmup"元素:

inbound_services:
- warmup

这将向 /_ah/warmup 发送请求,如果您愿意,您可以在此处执行实例所需的任何其他初始化(例如启动数据库连接池)。

有更多策略可以帮助您减少应用程序的延迟。

您可以修改 automatic_scaling options 以使用更适合您的应用程序的内容。

您可以通过 setting the appropriate Cache-Control header on your responses and set reasonable expiration times for static files 更好地管理您的带宽。

Using public Cache-Control headers in this way will allow proxy servers and your clients' browser to cache responses for the designated period of time.

您可以使用像 F2 这样更大的实例 class 来 避免水平缩放 经常发生。正如我从这个问题中了解到的那样,您的延迟主要在部署新实例时增加。

您也可以 enable concurrent requests 并尽可能将代码编写为 异步