【问题标题】:Google App Engine on startup autofill initial data in google data storeGoogle App Engine 在启动时自动填充 Google 数据存储中的初始数据
【发布时间】:2019-05-11 13:36:35
【问题描述】:

我有一个 Google App Engine Python 应用程序。我使用谷歌数据存储作为数据库来服务器动态内容。我想在启动谷歌应用引擎应用程序时自动填充谷歌数据存储中的一些数据。

启动我正在使用的应用程序

dev_appserver.py app.yaml

我有一个 python 脚本来自动填充初始数据,但不知道如何配置该脚本以在启动应用程序时运行一次。

此外,我使用 webapp2 和 jinja2 模板构建了网站。

【问题讨论】:

    标签: python google-app-engine google-cloud-platform google-cloud-datastore


    【解决方案1】:

    要在开发环境中进行这项工作,最简单的方法是在您的 appengine_config.py 文件中运行代码(如果您没有这样的文件,请在应用程序的顶层创建它,在与app.yaml 相同的文件夹)。

    # Make this the last item in app.yaml - set up vendoring etc first.
    
    from models import FooModel
    
    # Delete existing records
    keys = FooModel.query().fetch(keys_only=True)
    ndb.delete_multi(keys)
    
    data = get_data_from_somewhere()
    
    # Assumes data is an iterable of dicts
    entities = [FooModel(**item) for item in data]
    ndb.put_multi(entities)
    

    了解这种方法在云中无法可靠运行。这是因为在云中可能有多个应用程序实例一直在启动和停止。最好设计您的应用,以便在实例启动时(甚至在每次部署之后)不需要重新初始化数据存储。

    【讨论】:

      猜你喜欢
      • 2011-11-10
      • 2011-05-26
      • 2010-11-03
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      相关资源
      最近更新 更多