liveness_scope

liveness_scope is a Python method for creating and opening a LivenessScope for running a block of code. Use this function to wrap a block of code using a with statement. For the duration of the with block, the liveness scope will be open and any liveness referents created will be automatically managed by it.

Syntax

Parameters

None.

Returns

A SimpleLivenessScope, which shares the preserve, manage, and unmanage interface of LivenessScope.

Examples

Liveness scopes give you fine-grained control over when ticking tables and their dependencies are removed from the update graph. The primary use case is limiting the lifetime of intermediate objects: when the scope is released (on block exit for a with statement), anything the scope manages that is no longer needed stops ticking.

preserve transfers ownership of an object to the next outer scope — typically the script session itself — so that it outlives the inner scope. Preserved objects keep their own parents and dependencies live for as long as the preserved object itself is live. Intermediate objects that are not preserved stop being live when the scope is released, but only if they are truly no longer needed at that point.

In this example, a function-generated blink table and an intermediate aggregation are created inside the scope. Only the final result is preserved; ownership transfers to the outer scope (typically the script session). blink_table and aggregated remain live because result depends on them — but they are now managed through result's liveness rather than the scope's. When result is eventually released, blink_table and aggregated stop updating too:

You can also create a LivenessScope explicitly when you need to release it programmatically rather than at block exit.

Using liveness_scope as a decorator automatically manages all liveness referents created during the function call — they stop updating when the function returns: