代码之家  ›  专栏  ›  技术社区  ›  Vipluv

增加运行google flexible app engine代码的时间延迟deadlineexcederror

  •  2
  • Vipluv  · 技术社区  · 6 年前

    作为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 engine就会停止。 我第一次试着按照 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
    

    它起作用了,我开始抓到一些死线以外的错误。但显然,我不能总是抓住像这样的死线。有时我会发现错误,有时却没有。我想最好的方法是增加代码允许运行的时间,而不是仅仅捕获异常。

    我试图通过添加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

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

    1 回复  |  直到 6 年前
        1
  •  10
  •   Vipluv    6 年前

    由于我没有得到任何答复,我继续寻找。我知道很多人也有类似的问题。

    首先要注意的是,gae flexible环境没有标准环境中的大多数标准约束。这意味着 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云项目控制台中的日志记录。我看到相关的错误实际上是 [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工人有默认的30秒超时

    把这个改成

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

    这里的超时是120秒,但根据一些尝试和错误,它可以被优化。然而,这解决了我运行一个比通常需要更长时间的代码的特殊问题