Tiwari Tiwari - 21 days ago 8
Python Question

Concurent Access to datastore in app engine

i want to know if db.run_in_transaction() acts as a lock for Data store operations
and helps in case of concurrent access on same entity.

Does in following code it is guarantied that a concurrent access will not cause a race and instead of creating new entity it will not do a over-write

Is db.run_in_transaction() correct/best way to do so

in following code i m trying to create new unique entity with following code

def txn(charmer=None):
new = None
key = my_magic() + random_part()
sk = Snake.get_by_name(key)
if not sk:
new = Snake(key_name=key, charmer= charmer)
new.put()
return new
db.run_in_transaction(txn, charmer)

Answer

That is a safe method. Should the same name get generated twice, only one entity would be created.

It sounds like you have already looked at the transactions documentation. There is also a more detailed description.

Check out the docs (specifically the equivalent code) on Model.get_or_insert, it answers exactly the question you are asking:

The get and subsequent (possible) put are wrapped in a transaction to ensure atomicity. Ths means that get_or_insert() will never overwrite an existing entity, and will insert a new entity if and only if no entity with the given kind and name exists.