Gunicorn Worker 在使用任何 firestore 功能时超时 - 甚至没有 get

Dan*_*ues 5 python google-app-engine websocket gunicorn google-cloud-firestore

我在本机模式下处理 Cloud Firestore 时遇到很多麻烦。我习惯于使用 NDB,但尝试将我的应用程序更改为灵活环境,因为我需要使用 websocket。但是,使用 Firestore 真的是一场噩梦。我必须使用 Tasks API 进行简单的批量提交,效果很好。但我无法从数据库中检索单个文档,无论是本地还是部署的。在本地,我不断得到:

[2019-08-31 20:05:17 -0300] [1951] [INFO] Booting worker with pid: 1951
[2019-08-31 20:05:47 -0300] [1927] [CRITICAL] WORKER TIMEOUT (pid:1951)
[2019-08-31 20:05:48 -0300] [1964] [INFO] Booting worker with pid: 1964
[2019-08-31 20:06:18 -0300] [1927] [CRITICAL] WORKER TIMEOUT (pid:1964)
[2019-08-31 20:06:19 -0300] [1977] [INFO] Booting worker with pid: 1977
Run Code Online (Sandbox Code Playgroud)

我有一个名为“联系人”的集合,其中包含一个名为 5599999999 的文档,其中包含一对值:“name”:“test”。另外,我在本地使用gunicorn运行,因为我需要websockets。

这是本地运行的命令:

gunicorn -b 127.0.0.1:8080 -k flask_sockets.worker  main:app --reload
Run Code Online (Sandbox Code Playgroud)

我的 app.yaml 文件:

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT -k flask_sockets.worker main:app

runtime_config:
  python_version: 3

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10
Run Code Online (Sandbox Code Playgroud)

主要.py

import googleapiclient.discovery
import asyncio
import google.cloud.exceptions
import firebase_admin
from google.cloud import firestore
from google.oauth2 import service_account
from firebase_admin import credentials, firestore
from flask import Flask, render_template
from flask_sockets import Sockets

cred = credentials.Certificate('service.json')
firebase_admin.initialize_app(cred)
app = Flask(__name__)
sockets = Sockets(app)

@app.route('/')
def index():

  teste = GetContacts()

  return str(teste.to_dict())

def GetContacts():
  db = firestore.client()
  contacts = db.collection(u'Contacts').document(u'5599999999').get()
  return contacts

Run Code Online (Sandbox Code Playgroud)

我只是希望它的输出是一对值。但它会无限期地运行...

现在尝试过,仅使用 Flask。运行流畅。完全没有问题而且速度非常快。但是,除了获取一对值之外,我还遇到了这种错误:

$ python main.py
 * Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 325-288-398
127.0.0.1 - - [01/Sep/2019 01:03:53] "GET / HTTP/1.1" 200 -
ERROR:root:An error occurred during a request.
Traceback (most recent call last):
  File "/Users/dr/env/lib/python3.7/site-packages/flask_sockets.py", line 40, in __call__
    handler, values = adapter.match()
  File "/Users/dr/env/lib/python3.7/site-packages/werkzeug/routing.py", line 1799, in match
    raise NotFound()
werkzeug.exceptions.NotFound: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/dr/env/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/dr/env/lib/python3.7/site-packages/flask/app.py", line 1791, in dispatch_request
    self.raise_routing_exception(req)
  File "/Users/dr/env/lib/python3.7/site-packages/flask/app.py", line 1774, in raise_routing_exception
    raise request.routing_exception
  File "/Users/dr/env/lib/python3.7/site-packages/flask/ctx.py", line 336, in match_request
    self.url_adapter.match(return_rule=True)
  File "/Users/dr/env/lib/python3.7/site-packages/werkzeug/routing.py", line 1799, in match
    raise NotFound()
werkzeug.exceptions.NotFound: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
127.0.0.1 - - [01/Sep/2019 01:03:53] "GET /favicon.ico HTTP/1.1" 200 -
INFO:werkzeug:127.0.0.1 - - [01/Sep/2019 01:03:53] "GET /favicon.ico HTTP/1.1" 200 -
Run Code Online (Sandbox Code Playgroud)

sno*_*ity 9

这与本期https://github.com/grpc/grpc/issues/4629gevent中描述的(used by flask_sockets) 和(used by firebase) 冲突有关,解决方案是添加以下代码grpc

import grpc.experimental.gevent as grpc_gevent
grpc_gevent.init_gevent()
Run Code Online (Sandbox Code Playgroud)


小智 0

Gunicorn 的默认超时设置是 30 秒,这与我在日志中看到的一致。我不认为这是一个gunicorn问题。您是否尝试过在没有gunicorn的情况下建立连接?