google-app-engine - 多次生成 ndb.Future 是否安全?

标签 google-app-engine app-engine-ndb

这样做安全吗?

@ndb.tasklet
def foo():
    # Note that I do not yield right here
    future = some_key.get_async()

    # Perform some other code

    if some_condition:
        # I need the get_async result here
        entity = yield future

    # I also need the get_async result here.
    # If some_condition was true, I would be yielding this future
    # for the second time. Is this ok?
    entity = yield future

请不要告诉我我可以只在函数顶部 yield 并在代码的其余部分使用 entity。我的实际功能比这更复杂一些,并且我有一些可能需要使用 entity 的条件代码路径,并且我希望在最近的时刻yield能够在后台获取实体时执行我的其他代码。

最佳答案

是的,很安全!

如果你检查ndb中的Future类,它会在完成时设置结果,并且多个yield或get_result将检查它是否完成并返回存储的结果。

google/appengine/ext/ndb/tasklets.py,SDK 1.7.4 中的第 324 行

关于google-app-engine - 多次生成 ndb.Future 是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14835696/

相关文章:

python - GAE Blobstore : upload blob along with other text fields

google-app-engine - 维护 NDB 数据库中属性的唯一性

java - 将 GWT SQL 示例部署到 AppEngine,在数据库上记录错误

php - 仅生产中的 Google App Engine 302 状态任务队列

python - 如何将 Python POST 数据转换为 JSON?

python - 可以更有效地编写此递归吗?

google-app-engine - 如何使用 ndb 使 blobstore 删除操作事务安全?

google-app-engine - App Engine 上 Cloud Datastore 的客户端库 - NDB 或 google-cloud-datastore

python - 谷歌云应用引擎 : 502 Bad Gateway (nginx) error with Flask App

python - 从 NDB 中的 ComputedProperty 函数返回列表的解决方法