增加运行Google灵活应用引擎延迟DeadlineExceededError代码的时间

Vip*_*luv 5 python-3.x google-app-engine-python google-flexible

作为API调用的一部分,我在Google App Engine上运行了一个功能.结构是这样的

import externalmod
...
...

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

return answer
Run Code Online (Sandbox Code Playgroud)

函数externalmod是一个复杂的算法(不是数据存储区,不是urlfetch,只是纯python),它适用于桌面上的每种可能情况,但对于app引擎上的一些输入情况,当调用端点时会出现以下错误

{
 "code": 13,
 "message": "BAD_GATEWAY",
 "details": [
  {
   "@type": "type.googleapis.com/google.rpc.DebugInfo",
   "stackEntries": [],
   "detail": "application"
  }
 ]
}
Run Code Online (Sandbox Code Playgroud)

在查看https://cloud.google.com/appengine/articles/deadlineexceedederrors并进行以下讨论之后: 如何增加Google App Engine请求计时器.默认值为60秒

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

我意识到这是因为如果任何代码运行超过60秒,App引擎将停止.我首先尝试根据Do Exception捕获DeadlineExceededError异常执行以下操作

from google.appengine.runtime import DeadlineExceededError
try:
   answer = externalmod.Method()
except DeadlineExceededError:
   answer = some_default
Run Code Online (Sandbox Code Playgroud)

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

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

 try:
   answer = externalmod.Method()
 except :
   answer = some_default
Run Code Online (Sandbox Code Playgroud)

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

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

runtime_config:
python_version: 3
resources:
  cpu: 2
  memory_gb: 4
manual_scaling:
  instances: 1
Run Code Online (Sandbox Code Playgroud)

也许这个问题Taskqueue用于FLEXIBLE app引擎中的长时间运行任务

也可以有一个类似的答案,但我不知道什么是任务问题,而且我也不能排队,因为我运行的关键功能是独立的,我不想仅仅针对某些情况将其分解.我只是增加60秒的限制会更容易.我怎样才能做到这一点?

Vip*_*luv 11

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

首先要注意的是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

那有什么可以解释这个超时?我检查了google云项目控制台中的logging -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
Run Code Online (Sandbox Code Playgroud)

gunicorn工作人员默认30秒超时

将此更改为

entrypoint: gunicorn -t 120 -b :$PORT main:app
Run Code Online (Sandbox Code Playgroud)

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