【问题标题】:GAE datastore does not refreshGAE 数据存储不刷新
【发布时间】:2013-04-07 23:30:49
【问题描述】:

当我更新 GAE 数据存储时,只有在浏览器刷新页面后才会显示正确的数据存储内容:

import os

from google.appengine.ext.webapp import template
from google.appengine.ext import db
import webapp2

#def testkey():
#  return db.Key.from_path('test', 'test')

class TestEntity(db.Model):
  testkey = db.StringProperty(multiline=False)
  testvalue = db.StringProperty(multiline=False)

class TestRefreshProblem(webapp2.RequestHandler):
  def get(self):
    testquery = TestEntity.all()#.ancestor(testkey())
    entities = testquery.run()
    template_values = {
      'entities': entities,
      }
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

class TestRefreshProblemPost(webapp2.RequestHandler):
  def post(self):
    # testEntity = TestEntity(parent=testkey())
    testEntity = TestEntity()
    testEntity.testkey = self.request.get('testkey')
    testEntity.testvalue = self.request.get('testvalue')
    testEntity.put()
    self.redirect('/')

app = webapp2.WSGIApplication([
    ('/', TestRefreshProblem),
    ('/pst', TestRefreshProblemPost)
], debug=True)

index.html 是:

<html>
  <body>
    <table border=0>
        <tr><td width=200>Key</td><td width=200>Value</td></tr>
        {% for entity in entities %}
          <tr>
            <td width=200>{{ entity.testkey|escape }}</td>
            <td width=200>{{ entity.testvalue|escape }}</td>
          </tr>
        {% endfor %}
      </table>
     <form action="/pst" method="post">
      <table>
        <td ><input type="text" name="testkey" size=30/></td>
        <td ><input type="text" name="testvalue" size=30/></td>
        <tr><td><input type="submit" value="Add entity"></td></tr>
      </table>
    </form>
  </body>
</html>

通过使用(虚拟)祖先(re:带有# 的行),问题消失了。在我看来这是一种奇怪的行为……没有祖先能解决吗?

【问题讨论】:

    标签: python google-app-engine refresh google-cloud-datastore ancestor


    【解决方案1】:

    这是由于eventual consistency 而出现的预期行为。

    实际上,因为您正在运行开发服务器,所以这只是对最终一致性的模拟——在真实的生产系统中,结果不会那么可预测。解决方法是一样的。

    【讨论】:

    • 非常感谢。现在确实有意义;-)
    猜你喜欢
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 2015-09-25
    • 2012-06-06
    • 2013-06-21
    • 1970-01-01
    相关资源
    最近更新 更多