Python 3.9 is now generally available.

google.appengine.ext.ndb.transaction

Run a callback in a transaction.

callback A function or tasklet to be called.
**ctx_options Transaction options.

Useful options include: retries=N: Retry up to N times (i.e. try up to N+1 times) propagation=: Determines how an existing transaction should be propagated, where can be one of the following: TransactionOptions.NESTED: Start a nested transaction (this is the default; but actual nested transactions are not yet implemented, so effectively you can only use this outside an existing transaction). TransactionOptions.MANDATORY: A transaction must already be in progress. TransactionOptions.ALLOWED: If a transaction is in progress, join it. TransactionOptions.INDEPENDENT: Always start a new parallel transaction. xg=True: On the High Replication Datastore, enable cross-group transactions, i.e. allow writing to up to 5 entity groups. read_only=True: Indicates a transaction will not do any writes, which potentially allows for more throughput.

WARNING: Using anything other than NESTED for the propagation flag can have strange consequences. When using ALLOWED or MANDATORY, if an exception is raised, the transaction is likely not safe to commit. When using INDEPENDENT it is not generally safe to return values read to the caller (as they were not read in the caller's transaction).

Whatever callback() returns.

Whatever callback() raises; datastore_errors.TransactionFailedError if the transaction failed.

Note:

To pass arguments to a callback function, use a lambda, e.g. def my_callback(key, inc): ... transaction(lambda: my_callback(Key(...), 1))