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

标签 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

最佳答案

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

使用 eventlet 异步工作线程(如果使用 Google 客户端库,则不建议使用 gevent)

pip install eventlet

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

CMD exec gunicorn --worker-class eventlet --workers 3 -b :$PORT main:app

Gunicorn Worker Configuration

或者,使用描述的实现 here使用 pubsub 和 worker 。

关于python - 如何在使用gunicorn的Google App Engine上运行长任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48160535/

相关文章:

google-app-engine - 如何将中断的部署回滚到 GAE

django - 110 : Connection timed out (Nginx/Gunicorn)

python - 如何根据代码的输出在 jupyter 笔记本中创建文本文件

python - Scrapy 蜘蛛 : dealing with pages that have incorrectly-defined character encoding

mysql - 将具有自动增量 ID 的 MySQL 数据迁移到 Google 数据存储区?

python - Google App Engine 的脚本处理程序

nginx - Docker-compose:nginx 无法与 django 和 Gunicorn 一起使用

python - 如何使 flask 异步响应客户端?

python - 如何表示 PyTorch LSTM 3D 张量?

减法时的 Python timedelta 行为