Firebase Firestore get()快照在第二次查询时冻结

时间:2018-06-26 14:46:15

标签: python firebase flask elastic-beanstalk google-cloud-firestore

我正在尝试从AWS Elastic Beanstalk部署中访问Firestore。 EB应用程序是使用Flask在Python中编写的,所有内容都可以在本地正常运行。但是,当我尝试部署到EB时,只有第一个数据库请求成功-第二个请求在调用.get()时冻结了我的应用程序。

我不确定这是EB的权限问题,Firestore问题还是什么!由于没有抛出异常,因此我什至无法拒绝请求并继续前进。

我确实在日志中得到以下内容:

[Tue Jun 26 11:29:16.964989 2018] [core:error] [pid 3914] [client 172.31.17.165:22711] Script timed out before returning headers: application.py

...但是那是因为脚本由于调用.get()而永不返回。

这是此测试应用程序的完整代码:

import os

from flask import Flask, url_for, redirect, render_template
import mimetypes
import time

import google
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

print("=====================================================")
print("initializing app")
cred = credentials.Certificate("/my/actual/path/to/credentials/is/here.json")
firebase_admin.initialize_app(cred)
print("done.")

print("initializing firestore")
db = firestore.client()
print("done.")

print("initializing flask")
application = Flask(__name__)
print("done.")

@application.route('/doc/<docId>')
def doc(docId):
    docId = docId.lower()

    documents_ref = db.collection(u'documents')
    doc_ref = documents_ref.document(docId)

    print("getting doc")
    doc = None
    try:
        doc = doc_ref.get()
    except google.cloud.exceptions.NotFound:
        print("not found.")
        return render_template('doc.html', message="This document no longer exists .")
    print("done.")

    # Here is where I'd normally use the DB snapshot data, but omitted for the question
    creatorStr = "Someone"
    timeStr = "a time"

    docMessage = creatorStr + " sent you a document dated " + timeStr + "."

    return render_template('doc.html', docMessage=docMessage)

if __name__ == "__main__":
    # application.debug = True
    application.run()

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

好吧,我终于找到了解决这个问题的方法: AWS Elastic Beanstalk - Script timed out before returning headers: application.py

像这样创建一个ebextension文件,然后重新部署以解决该问题:

nano .ebextensions/<some_name>.config 

#add the following to <some_name>.config 
files:
  "/etc/httpd/conf.d/wsgi_custom.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      WSGIApplicationGroup %{GLOBAL}


#add to git
git add .ebextensions/<some_name>.config
git commit -m 'message here'

#deploy to beanstalk
eb deploy