GAE NDB"结果无法设置两次"错误

pat*_*atb 5 python google-app-engine app-engine-ndb google-cloud-datastore

我点击这个结果不能设置两次运行时错误GAE Ndb异步查询在做ndb.Future.wait_all(futures)一堆异步查询时.

像这样的东西:

futures = []
for item in items:
    item._future_get = MyEntity.query(...).get_async()
    futures.append(item._future_get)

ndb.Future.wait_all(futures)
# ...
Run Code Online (Sandbox Code Playgroud)

它失败了wait_allwith with result无法设置两次

SO上没有提到此错误消息.谷歌在2011年有2-3次提及它,并没有明确的解释.

更多信息:

items是前一次获取的ndb实体.但它们在这里并不重要(至少我认为),因为查询是在MyEntity上执行的.我习惯以这种方式将期货附加到它们所涉及的对象上,因此当所有已完成时更容易理清.

堆栈跟踪:

  File "/home/my_project/app/main/admin/my_module.py", line 166, in admin_base_cleanup_details ndb.Future.wait_all(futures)
  File "/usr/lib/python2.7/google_appengine/google/appengine/ext/ndb/tasklets.py", line 350, in wait_all ev.run1()
  File "/usr/lib/python2.7/google_appengine/google/appengine/ext/ndb/eventloop.py", line 235, in run1 delay = self.run0()
  File "/usr/lib/python2.7/google_appengine/google/appengine/ext/ndb/eventloop.py", line 197, in run0 callback(*args, **kwds)
INFO     2016-04-26 08:40:04,152 module.py:808] default: "GET /admin/cleanup/details?mode=status HTTP/1.1" 500 -
  File "/usr/lib/python2.7/google_appengine/google/appengine/ext/ndb/tasklets.py", line 475, in _on_future_completion self._help_tasklet_along(ns, ds_conn, gen, val)
  File "/usr/lib/python2.7/google_appengine/google/appengine/ext/ndb/tasklets.py", line 386, in _help_tasklet_along self.set_result(result)
  File "/usr/lib/python2.7/google_appengine/google/appengine/ext/ndb/tasklets.py", line 265, in set_result
    raise RuntimeError('Result cannot be set twice.')
RuntimeError: Result cannot be set twice.
Run Code Online (Sandbox Code Playgroud)

更精确一些:

  • 是的,它确实发生在GAE和本地开发上.

  • 不,它不会每次都失败,但通常就足够了.

我发现它与另一个线程的并发性有关.该网页通过ajax调用启动了2个请求:一个用于带有一些异步调用的更新查询,这需要几秒钟,另一个像周期性状态更新,更快,但也有异步调用.后者失败了,并非总是但经常失败.从那时起,我避免重叠这两个请求,并且它停止了失败.它似乎仍然是一个错误,因为重叠请求不是禁止的.

小智 1

您正在使用 get_async() ,它“异步返回第一个查询结果”,而您可能应该使用 fetch_async() 来获取 Future。

https://cloud.google.com/appengine/docs/python/ndb/queryclass#Query_get_async