Create / interact with a batch of updates / deletes.
Batches provide the ability to execute multiple operations in a single request to the Cloud Datastore API.
See https://cloud.google.com/datastore/docs/concepts/entities#batch_operations
Classes
Batch
Batch
(
client
)
An abstraction representing a collected group of updates / deletes.
Used to build up a bulk mutation.
For example, the following snippet of code will put the two save
operations and the delete
operation into the same mutation, and send
them to the server in a single API request::
from google.cloud import datastore client = datastore.Client() batch = client.batch() batch.begin() batch.put(entity1) batch.put(entity2) batch.delete(key3) batch.commit()
You can also use a batch as a context manager, in which case commit
will be called automatically if its block exits without
raising an exception::
with batch: ... batch.put(entity1) ... batch.put(entity2) ... batch.delete(key3)
By default, no updates will be sent if the block exits with an error::
with batch: ... do_some_work(batch) ... raise Exception() # rolls back