Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在事务中,我如何读取我刚刚编写的实体?_Python_Google App Engine_Google Cloud Datastore - Fatal编程技术网

Python 在事务中,我如何读取我刚刚编写的实体?

Python 在事务中,我如何读取我刚刚编写的实体?,python,google-app-engine,google-cloud-datastore,Python,Google App Engine,Google Cloud Datastore,我正在使用Python2.7、GAE和高复制数据存储。 我正在尝试执行一个事务,该事务首先写入一个实体,然后读取它,但读取过程从未找到该实体。这是我的一个测试用例: class DemoTestCase(unittest.TestCase): def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.policy = datastore_stub_util.Pseudo

我正在使用Python2.7、GAE和高复制数据存储。 我正在尝试执行一个事务,该事务首先写入一个实体,然后读取它,但读取过程从未找到该实体。这是我的一个测试用例:

class DemoTestCase(unittest.TestCase):
  def setUp(self):
    self.testbed = testbed.Testbed()
    self.testbed.activate()
    self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=0)
    self.testbed.init_datastore_v3_stub(consistency_policy=self.policy)

  def tearDown(self):
    self.testbed.deactivate()

  def test1(self):
    db.run_in_transaction(self._write)
    db.run_in_transaction(self._read)

  def test2(self):
    db.run_in_transaction(self._write_read)

  def _write(self):
    self.root_key = db.Key.from_path("A", "root")
    a             = A(a=1, parent=self.root_key)
    self.a_key    = a.put()

  def _read(self):
    b = sample.read_a(self.a_key)
    self.assertEqual(1, b.a)
    self.assertEqual(1, A.all().ancestor(self.root_key).count(5))

  def _write_read(self):
    root_key = db.Key.from_path("A", "root")
    a     = A(a=1, parent=root_key)
    a_key = a.put()
    b     = sample.read_a(a_key)
    self.assertEqual(None, b)
    self.assertEqual(0, A.all().ancestor(root_key).count(5))
两个测试用例现在都通过了

Test1正在运行一个执行写操作的事务。然后运行第二个事务,执行两次读取,一次按键读取,一次按祖先查询。在这种情况下,阅读效果很好

Test2运行的代码与test1完全相同,但这次所有代码都在同一事务中运行。如您所见,按键读取实体将返回None。执行祖先查询返回0个命中

我的问题是:在事务中,我如何读取我刚刚编写的实体?或者这是不可能的


谢谢。

你不能。事务内的所有数据存储读取在事务启动时显示数据存储的快照。书写不会出现


理论上,你不应该读,因为你写的每个实体都有一个实例。使用那个实例。

你不能。事务内的所有数据存储读取在事务启动时显示数据存储的快照。书写不会出现


理论上,你不应该读,因为你写的每个实体都有一个实例。使用那个例子。

嗯,有时候重新阅读确实很有帮助。业务规则可能由实体更新触发,需要重新加载。业务代表通常不知道是什么触发了他们,因此无法立即访问新实体


不知道Python的情况,但是在使用Objectify的Java中,更新在transaction by Objectify会话(transaction)缓存中可见。如果您正在使用的Python持久性框架中有类似于会话缓存的东西,那么这可能是一个解决方案。

嗯,有时候重新读取确实很有帮助。业务规则可能由实体更新触发,需要重新加载。业务代表通常不知道是什么触发了他们,因此无法立即访问新实体


不知道Python的情况,但是在使用Objectify的Java中,更新在transaction by Objectify会话(transaction)缓存中可见。如果您正在使用的Python持久性框架中有类似于会话缓存的东西,那么这可能是一个解决方案。

还请注意,如果您正在使用NDB(您应该:-),并且针对要匹配的特定写和读操作启用了上下文缓存,那么您将从上下文缓存中读取您所写的内容。(但是NDB永远不会在事务中从memcache中读取。)还要注意,如果您使用NDB(您应该:-),并且为要匹配的特定写和读操作启用了上下文内缓存,那么您将从上下文内缓存中读取您所写的内容。(但是,NDB永远不会在事务中从memcache读取。)