【问题标题】:Increase time to run code for Google flexible app engine delaying DeadlineExceededError为延迟 DeadlineExceededError 的 Google 灵活应用引擎增加运行代码的时间
【发布时间】:2018-11-13 00:51:40
【问题描述】:

作为 API 调用的一部分,我在 Google App Engine Flexible 上运行了一个函数。结构是这样的

import externalmod
...
...

@app.route('/calc_here')
def calc:
answer = externalmod.Method()

return answer

函数 externalmod 是一个复杂的算法(不是数据存储,不是 urlfetch,只是纯 python),它适用于桌面上的所有可能情况,但对于应用程序引擎上的某些输入情况,当调用端点时,它会给出以下信息错误

{
 "code": 13,
 "message": "BAD_GATEWAY",
 "details": [
  {
   "@type": "type.googleapis.com/google.rpc.DebugInfo",
   "stackEntries": [],
   "detail": "application"
  }
 ]
}

查看https://cloud.google.com/appengine/articles/deadlineexceedederrors 和以下讨论后: How to increase Google App Engine request timer. Default is 60 sec

https://groups.google.com/forum/#!topic/google-appengine/3TtfJG0I9nA

我意识到这是因为如果任何代码运行超过 60 秒,App 引擎就会停止。 我首先尝试根据Should Exception catch DeadlineExceededError exceptions?做以下操作

from google.appengine.runtime import DeadlineExceededError
try:
   answer = externalmod.Method()
except DeadlineExceededError:
   answer = some_default

但我得到了没有模块 google.appengine 的错误

然后意识到所有文档都是针对标准环境的,但我使用的是灵活的环境,我认为这个 appengine.runtime 可能甚至不再存在 当我这样做时:

 try:
   answer = externalmod.Method()
 except :
   answer = some_default

它起作用了,我开始发现一些 DeadlineExceededErrors。但显然,我不能总是像这样捕获 DeadlineExceededErrors。有时我会发现错误,有时不会。我认为最好的方法是增加允许代码运行的时间,而不是仅仅捕获异常。

我尝试通过添加 CPU:2 来更改 app.yaml 文件,但没有任何区别。

runtime_config:
python_version: 3
resources:
  cpu: 2
  memory_gb: 4
manual_scaling:
  instances: 1

也许这个问题Taskqueue for long running tasks in FLEXIBLE app engine

也可能有类似的答案,但我不知道任务队列是什么,而且我不能排队任何东西,因为我正在运行的关键功能是独立的,我不想只在某些情况下分解它。对我来说,增加 60 秒的限制会更容易。我该怎么做?

【问题讨论】:

    标签: python-3.x google-app-engine-python google-flexible


    【解决方案1】:

    由于我没有得到任何答案,我继续搜索。我意识到许多其他人也有类似的问题。

    首先要注意的是,GAE 柔性环境不像标准环境那样具有大多数标准约束。这意味着DeadlineExceededError 不存在,因为没有 60 秒的最后期限。所有模块和代码都像在任何计算机上一样运行,因为它们都包含在 Docker 容器中。

    https://cloud.google.com/appengine/docs/flexible/python/migrating

    此外,没有 google.appengine 模块。根据所使用的语言,所有云交互都应通过 google.cloud API https://cloud.google.com/apis/docs/overview 进行

    那么什么可以解释这个超时呢?我检查了谷歌云项目控制台中的日志记录-logs。我看到相关错误实际上是[CRITICAL] WORKER TIMEOUT,它发生在函数被调用后正好30秒。这与 GAE flex 无关,而是与服务器框架有关。在我的情况下,'gunicorn'。

    这里提供的答案其实是https://serverfault.com/questions/490101/how-to-resolve-the-gunicorn-critical-worker-timeout-error/627746

    基本上,使用文档http://docs.gunicorn.org/en/latest/settings.html#config-file

    唯一需要的更改是在 app.yaml 文件中

    早在哪里

    runtime: python
    env: flex
    entrypoint: gunicorn -b :$PORT main:app
    

    gunicorn worker 有一个默认的 30 秒超时时间

    把这个改成

    entrypoint: gunicorn -t 120 -b :$PORT main:app
    

    这里的超时时间是 120 秒,但可以根据一些试验和错误进行优化。然而,这解决了我运行代码比平时花费更长的特殊问题

    【讨论】:

    • 注意gunicorn必须列在requrements.txt中,否则应用程序将无法运行,日志中会填满消息:/bin/sh: 1: exec: gunicorn: not found
    猜你喜欢
    • 2012-02-04
    • 2020-08-17
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2016-08-01
    • 2018-11-04
    • 1970-01-01
    • 2019-05-11
    相关资源
    最近更新 更多