Context

Context for currently running tasks and transactions.

class google.cloud.ndb.context.Context(client, id=None, 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, transaction_complete_callbacks=None, legacy_data=True, retry=None, 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(id, client, namespace, eventloop, batches, commit_batches, transaction, cache, global_cache, on_commit_callbacks, transaction_complete_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.

call_on_transaction_complete(callback)[source]

Call a callback upon completion 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 completes, in the order in which they were registered. Callbacks are called regardless of whether transaction is committed or rolled back.

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 or rolled back at that point.)

Parameters

callback (Callable) – The callback function.

clear_cache()[source]

Clears the in-memory cache.

This does not affect global cache.

flush()[source]

Force any pending batch operations to go ahead and run.

get_cache_policy()[source]

Return the current context cache policy function.

Returns

A function that accepts a

Key instance as a single positional argument and returns a bool indicating if it should be cached. May be None.

Return type

Callable

get_datastore_policy()[source]

Return the current context datastore policy function.

Returns

A function that accepts a

Key instance as a single positional argument and returns a bool indicating if it should use the datastore. May be None.

Return type

Callable

get_global_cache_policy()[source]

Return the current global cache policy function.

Returns

A function that accepts a

Key instance as a single positional argument and returns a bool indicating if it should be cached. May be None.

Return type

Callable

get_global_cache_timeout_policy()[source]

Return the current policy function global cache timeout (expiration).

Returns

A function that accepts a

Key instance as a single positional argument and returns an int indicating the timeout, in seconds, for the key. 0 implies the default timeout. May be None.

Return type

Callable

get_memcache_policy()

Return the current global cache policy function.

Returns

A function that accepts a

Key instance as a single positional argument and returns a bool indicating if it should be cached. May be None.

Return type

Callable

get_memcache_timeout_policy()

Return the current policy function global cache timeout (expiration).

Returns

A function that accepts a

Key instance as a single positional argument and returns an int indicating the timeout, in seconds, for the key. 0 implies the default timeout. May be None.

Return type

Callable

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

str

in_retry()[source]

Get whether we are already in a retry block.

Returns

True if currently in a retry block, otherwise

False.

Return type

bool

in_transaction()[source]

Get whether a transaction is currently active.

Returns

True if currently in a transaction, otherwise

False.

Return type

bool

memcache_add(*args, **kwargs)[source]

Direct pass-through to memcache client. No longer implemented.

memcache_cas(*args, **kwargs)[source]

Direct pass-through to memcache client. No longer implemented.

memcache_decr(*args, **kwargs)[source]

Direct pass-through to memcache client. No longer implemented.

memcache_delete(*args, **kwargs)[source]

Direct pass-through to memcache client. No longer implemented.

memcache_get(*args, **kwargs)[source]

Direct pass-through to memcache client. No longer implemented.

memcache_gets(*args, **kwargs)[source]

Direct pass-through to memcache client. No longer implemented.

memcache_incr(*args, **kwargs)[source]

Direct pass-through to memcache client. No longer implemented.

memcache_replace(*args, **kwargs)[source]

Direct pass-through to memcache client. No longer implemented.

memcache_set(*args, **kwargs)[source]

Direct pass-through to memcache client. No longer implemented.

set_cache_policy(policy)[source]

Set the context cache policy function.

Parameters

policy (Callable) – A function that accepts a Key instance as a single positional argument and returns a bool indicating if it should be cached. May be None.

set_datastore_policy(policy)[source]

Set the context datastore policy function.

Parameters

policy (Callable) – A function that accepts a Key instance as a single positional argument and returns a bool indicating if it should use the datastore. May be None.

set_global_cache_policy(policy)[source]

Set the global cache policy function.

Parameters

policy (Callable) – A function that accepts a Key instance as a single positional argument and returns a bool indicating if it should be cached. May be None.

set_global_cache_timeout_policy(policy)[source]

Set the policy function for global cache timeout (expiration).

Parameters

policy (Callable) – A function that accepts a Key instance as a single positional argument and returns an int indicating the timeout, in seconds, for the key. 0 implies the default timeout. May be None.

set_memcache_policy(policy)

Set the global cache policy function.

Parameters

policy (Callable) – A function that accepts a Key instance as a single positional argument and returns a bool indicating if it should be cached. May be None.

set_memcache_timeout_policy(policy)

Set the policy function for global cache timeout (expiration).

Parameters

policy (Callable) – A function that accepts a Key instance as a single positional argument and returns an int indicating the timeout, in seconds, for the key. 0 implies the default timeout. May be None.

urlfetch(*args, **kwargs)[source]

Fetch a resource using HTTP. No longer implemented.

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 to False in order to have it just return None if called outside of a context. Default: True

Returns

The current context.

Return type

Context

Raises

ContextError – If called outside of a context established by google.cloud.ndb.client.Client.context() and raise_context_error is True.

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 superseded 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 to False in order to have it just return None if called outside of a context. Default: True

Returns

The current context.

Return type

Context

Raises

ContextError – If called outside of a context established by google.cloud.ndb.client.Client.context() and raise_context_error is True.