【问题标题】:Google App Engine ndb: Order of mixed sync and async operationsGoogle App Engine ndb:混合同步和异步操作的顺序
【发布时间】:2018-08-10 10:54:34
【问题描述】:

在使用 Google App Engine ndb 时,我是否需要担心在同一个函数中混合同步和异步 put 操作?

例如假设我有一些这样的代码:

class Entity(ndb.Model):
  some_flag = ndb.BooleanProperty()


def set_flag():
  ent=Entity()

  ent.some_flag = False
  ent.put_async()

  ent.some_flag = True
  ent.put()

该数据存储是否负责确保在同步写入之前应用所有挂起的异步写入(以便在set_flag 运行之后,保证标志将为True)?还是因为异步 put 可能在同步 put 之后完成而存在竞争条件?

【问题讨论】:

    标签: google-app-engine app-engine-ndb google-app-engine-python


    【解决方案1】:

    不,数据存储不会为您处理这些问题。

    即使使用同步 put,来自不同线程的调用也可能相互覆盖。

    我建议您阅读一些有关交易的内容,以及何时以及为什么会有帮助。

    【讨论】:

      【解决方案2】:

      对于示例代码和实用解决方案,您可以查看 Dan McGrath 对“Cloud Datastore:避免竞争条件的方法”question 的回复。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-28
        • 1970-01-01
        相关资源
        最近更新 更多