1

When using Google App Engine ndb, do I have to worry about mixing synchronous and asynchronous put operations in the same function?

e.g. Say that I have some code like this:

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()

Does that datastore take care of ensuring that all pending async writes are applied before the synchronous write (so that after set_flag runs, it is guaranteed that the flag will be True)? Or is there a race condition because the async put might complete after the synchronous put?

alzy
  • 307
  • 1
  • 10

2 Answers2

2

No, the datastore does not take care of this for you.

Even with synchronous puts, calls from different threads can overwrite each other.

I recommend that you read up a bit on transactions, and when and why there are helpful.

new name
  • 15,861
  • 19
  • 68
  • 114
0

For sample code, and a practical solution, you may have a look at Dan McGrath's reply to the "Cloud Datastore: ways to avoid race conditions" question.

George
  • 1,488
  • 1
  • 10
  • 13