Context¶
Context for currently running tasks and transactions.
-
class
google.cloud.ndb.context.
Context
(client, namespace=<object object>, eventloop=None, batches=None, commit_batches=None, transaction=None, cache=None, cache_policy=None, global_cache=None, global_cache_policy=None, global_cache_timeout_policy=None, datastore_policy=None, on_commit_callbacks=None, legacy_data=True, rpc_time=None, wait_time=None)[source]¶ Bases:
google.cloud.ndb.context._Context
User management of cache and other policy.
Create new instance of _ContextTuple(client, namespace, eventloop, batches, commit_batches, transaction, cache, global_cache, on_commit_callbacks, legacy_data)
-
call_on_commit
(callback)[source]¶ Call a callback upon successful commit of a transaction.
If not in a transaction, the callback is called immediately.
In a transaction, multiple callbacks may be registered and will be called once the transaction commits, in the order in which they were registered. If the transaction fails, the callbacks will not be called.
If the callback raises an exception, it bubbles up normally. This means: If the callback is called immediately, any exception it raises will bubble up immediately. If the call is postponed until commit, remaining callbacks will be skipped and the exception will bubble up through the transaction() call. (However, the transaction is already committed at that point.)
- Parameters
callback (Callable) – The callback function.
-
get_global_cache_timeout_policy
()[source]¶ Return the current policy function global cache timeout (expiration).
-
get_memcache_policy
()¶ Return the current global cache policy function.
-
get_memcache_timeout_policy
()¶ Return the current policy function global cache timeout (expiration).
-
get_namespace
()[source]¶ Return the current context namespace.
If namespace isn’t set on the context, the client’s namespace will be returned.
- Returns
The namespace, or None.
- Return type
-
set_global_cache_timeout_policy
(policy)[source]¶ Set the policy function for global cache timeout (expiration).
-
set_memcache_policy
(policy)¶ Set the global cache policy function.
-
set_memcache_timeout_policy
(policy)¶ Set the policy function for global cache timeout (expiration).
-
-
google.cloud.ndb.context.
get_context
(raise_context_error=True)[source]¶ Get the current context.
This function should be called within a context established by
google.cloud.ndb.client.Client.context()
.- Parameters
raise_context_error (bool) – If set to
True
, will raise an exception if called outside of a context. Set this toFalse
in order to have it just returnNone
if called outside of a context. Default:True
- Returns
The current context.
- Return type
- Raises
ContextError – If called outside of a context established by
google.cloud.ndb.client.Client.context()
andraise_context_error
isTrue
.
-
google.cloud.ndb.context.
get_toplevel_context
(raise_context_error=True)[source]¶ Get the current top level context.
This function should be called within a context established by
google.cloud.ndb.client.Client.context()
.The toplevel context is the context created by the call to
google.cloud.ndb.client.Client.context()
. At times, this context will be superceded by subcontexts, which are used, for example, during transactions. This function will always return the top level context regardless of whether one of these subcontexts is the current one.- Parameters
raise_context_error (bool) – If set to
True
, will raise an exception if called outside of a context. Set this toFalse
in order to have it just returnNone
if called outside of a context. Default:True
- Returns
The current context.
- Return type
- Raises
ContextError – If called outside of a context established by
google.cloud.ndb.client.Client.context()
andraise_context_error
isTrue
.