如何在使用 gunicorn 的 Google App Engine 上运行长任务?

Joe*_* C. 5 python google-app-engine gunicorn

GAE flex 默认使用 gunicorn 作为入口点,这很好,除了我有一个函数需要很长时间来处理(抓取网站和数据库中的故事数据)并且默认情况下 gunicorn 在 30 秒超时,然后是一个新工人从头开始执行任务,依此类推。

我可以将 gunicorn 超时设置为 20 分钟之类的时间,但这似乎并不优雅。有没有办法在 gunicorn 的“外部”运行这些后端功能,或者我没有考虑过的 gunicorn 配置?没有客户端,所以完成时间长不是问题。

我的app.yaml文件目前看起来像这样:

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

runtime_config:
  python_version: 2

# This sample incurs costs to run on the App Engine flexible environment. 
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your app-with-app-yaml
manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 3
  disk_size_gb: 10
Run Code Online (Sandbox Code Playgroud)

Rob*_*tis 1

您可以使用异步工作级,然后无需将超时设置为 20 分钟。默认的工作线程类是sync。关于这里的工人的文件。

使用 eventlet 异步工作线程(如果使用 google 客户端库,则不推荐使用 gevent)

pip install eventlet
Run Code Online (Sandbox Code Playgroud)

然后在你的gunicorn实例化中设置worker-class = 'eventlet'并将worker数量设置为[核心数] x 2 +1(这只是谷歌文档中的建议)。例如:

CMD exec gunicorn --worker-class eventlet --workers 3 -b :$PORT main:app
Run Code Online (Sandbox Code Playgroud)

Gunicorn工人配置

或者,使用此处描述的使用 pubsub 和工人的实现。