【问题标题】:Google App Engine - Flask - SQLAlchemy - Inconsistent DatabaseGoogle App Engine - Flask - SQLAlchemy - 不一致的数据库
【发布时间】:2017-07-17 23:45:26
【问题描述】:

在我的本地服务器上,应用程序数据库运行良好并且总是显示相同的结果,但是当我将 Flask 应用程序部署到服务器并添加一个项目时,当我检索数据时结果会有所不同。您可以在http://rest-menuapp.appspot.com/JSON 上查看此问题。如果您刷新页面,您会看到该项目有时不存在,有时存在。

这是我的 app.yaml 文件:

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT item_catalog:app
threadsafe: false
runtime_config:
  python_version: 2

handlers:
- url: /static
  static_dir: static

- url: /static/photos
  static_dir: static/photos

- url: /.*
  script: item_catalog.app

这是我的数据库

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base


engine = create_engine('sqlite:///catalog.db', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
                                         autoflush=False,
                                         bind=engine))

Base = declarative_base()
Base.query = db_session.query_property()

def init_db():
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    import models
    Base.metadata.create_all(bind=engine)

这是我的路由python文件的开始

from flask import Flask, render_template, request, redirect, jsonify,\
     url_for, session, g, send_from_directory
from sqlalchemy import create_engine, asc
from sqlalchemy.orm import sessionmaker
from datetime import datetime
from functools import wraps
from werkzeug.utils import secure_filename
from PIL import Image
import os



from apiclient import discovery
import httplib2
from oauth2client import client, crypt

from database import init_db, db_session
from models import Item, Category, User, Photo

UPLOAD_FOLDER = 'static/photos'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
init_db()


app = Flask(__name__)

【问题讨论】:

  • 您将状态存储在本地文件中。如果您有多个实例运行您的应用程序,您将拥有多个数据副本,每个副本彼此不同。
  • 这是catalog.db。我将如何更改以将其保存为所有实例。
  • 我只需要设置一个 postgresql 数据库吗?或者我怎样才能关闭多个实例?
  • 我推荐使用 PostgreSQL。

标签: python google-app-engine flask sqlalchemy


【解决方案1】:

因此,快速简便的解决方案是将我的谷歌应用引擎更改为仅使用一个实例。这是通过添加:

manual_scaling:
  instances: 1

到我的 app.yaml 文件。

正如 cmets 中提到的 univerio,我将数据库文件存储在本地,并且由于有多个实例,因此会有多个版本的数据库。另一个解决方案是将我的数据库切换到 PostgreSQL。就我的目的而言,简单地限制实例数量就足够了。

【讨论】:

    猜你喜欢
    • 2010-10-16
    • 2020-09-12
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    相关资源
    最近更新 更多