gpt4 book ai didi

python - GAE NDB "Result cannot be set twice"错误

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:42 25 4
gpt4 key购买 nike

在对一堆异步查询执行 ndb.Future.wait_all(futures) 时,我遇到了这个 Result cannot be set twice GAE Ndb 异步查询运行时错误。

像这样:

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

ndb.Future.wait_all(futures)
# ...

wait_all 失败,Result cannot be set twice

SO 上没有提及此错误消息。自 2011 年以来,谷歌曾有 2-3 次提及它,但没有明确的解释。

更多信息:

items 是之前获取的 ndb 实体。但它们在这里并不重要(至少我认为),因为查询是在 MyEntity 上执行的。我习惯于以这种方式将 futures 附加到它们相关的对象上,所以当所有完成时更容易整理。

堆栈跟踪:

  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.

一些更精确的:

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

  • 不,它不会每次都失败,但足够频繁了。

我发现它与另一个线程的并发有关。该网页通过 ajax 调用启动了 2 个请求:一个用于带有一些异步调用的更新查询,这将花费相当多的几秒钟,另一个类似于定期状态更新,速度更快但也带有异步调用。失败的是后者,并非总是如此,但经常如此。从那以后,我避免了两个请求的重叠,它不再失败了。它仍然看起来像一个错误,因为重叠请求不是禁止的。

最佳答案

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

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

关于python - GAE NDB "Result cannot be set twice"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36849031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com