簡體   English   中英

Google App Engine-Flask-SQLAlchemy-不一致的數據庫

[英]Google App Engine - Flask - SQLAlchemy - Inconsistent Database

在本地服務器上,應用程序數據庫運行良好,並且始終顯示相同的結果,但是當我將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__)

因此,快速簡便的解決方案是將我的Google App引擎更改為僅使用一個實例。 通過添加以下內容來完成此操作:

manual_scaling:
  instances: 1

到我的app.yaml文件。

正如評論中提到的univerio一樣,我將數據庫文件存儲在本地,並且由於存在多個實例,因此會有多個版本的數據庫。 另一個解決方案是將數據庫切換到PostgreSQL。 出於我的目的,僅限制實例數量就足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM