def get_ndb_context(namespace=None, global_cache=None): # type: (Optional[str], Optional[RedisCache]) -> ndb.context.Context """Get the context of the Cloud NDB. This context needs to be entered in order to do any Cloud NDB operations. Returns: ndb.context.Context. Cloud NDB context. """ # The raise_context_error arg is set to False, because we want to generate # a new context if we are outside of one. This is used because in some # places we need a context but we are unsure if it exists. context = ndb.get_context(raise_context_error=False) return (CLIENT.context(namespace=namespace, global_cache=global_cache) if context is None else contextlib.nullcontext(enter_result=context))
def callback(): transaction = ndb.get_context().transaction yield ndb.sleep(delay) assert ndb.get_context().transaction == transaction raise ndb.Return(transaction)
def save_entity(): ndb.get_context().call_on_commit(commit_callback) entity = SomeKind(foo=42, bar="none") key = entity.put() dispose_of(key._key) return key