A client for NDB which manages credentials, project, namespace.
Client(project=None, namespace=None, credentials=None)¶
An NDB client.
The NDB client must be created in order to use NDB, and any use of NDB must be within the context of a call to
project (Optional[str]) – The project to pass to proxied API methods. If not passed, falls back to the default inferred from the environment.
namespace (Optional[str]) – Namespace to pass to proxied API methods.
Credentials]) – The OAuth2 Credentials to use for this client. If not passed, falls back to the default inferred from the environment.
The scopes required for authenticating as a Cloud Datastore consumer.
context(namespace=<object object>, cache_policy=None, global_cache=None, global_cache_policy=None, global_cache_timeout_policy=None, legacy_data=True)¶
Establish a context for a set of NDB calls.
This method provides a context manager which establishes the runtime state for using NDB.
from google.cloud import ndb client = ndb.Client() with client.context(): # Use NDB for some stuff pass
Use of a context is required–NDB can only be used inside a running context. The context is used to manage the connection to Google Cloud Datastore, an event loop for asynchronous API calls, runtime caching policy, and other essential runtime state.
Code within an asynchronous context should be single threaded. Internally, a
threading.localinstance is used to track the current event loop.
In a web application, it is recommended that a single context be used per HTTP request. This can typically be accomplished in a middleware layer.
legacy_data (bool) – Set to
True(the default) to write data in a way that can be read by the legacy version of NDB.