Eventum Logo

Eventum

Eventtemplate

State & Subprocess

Persistent state scopes (locals, shared, globals) and subprocess execution in Jinja2 templates.

State

Templates can store and retrieve values that persist across renders. State is organized into three scopes:

ScopeVariableVisibilityThread safetyTypical use
LocallocalsCurrent template onlySingle-threadedPer-template counters, accumulators
SharedsharedAll templates in one generatorSingle-threadedCross-template coordination, session tracking
GlobalglobalsAll generators in the applicationThread-safe (RLock)Global counters, inter-generator data

State API

All three scopes provide the same methods:

MethodSignatureDescription
get(key, default=None) → AnyGet a value. Returns default if the key doesn't exist.
set(key, value) → NoneSet a value.
pop(key, default=None) → AnyRemove a key and return its value. Returns default if the key doesn't exist.
update(mapping) → NoneSet multiple values at once from a dict.
clear() → NoneRemove all values.
as_dict() → dictGet a shallow copy of the entire state.
[key]bracket accessSame as get(key).

The globals scope has two additional methods for manual locking:

MethodDescription
acquire()Acquire the state lock.
release()Release the state lock.

Individual globals operations (get, set, etc.) are already thread-safe. Use acquire() / release() only when you need multiple operations to execute atomically — for example, reading a counter and incrementing it without another generator modifying it in between.

Use Scenarios in Eventum Studio to visualize which generators read and write global state keys, and to manage global state values at runtime.

Examples

Per-template counter with locals:

{%- do locals.set('n', locals.get('n', 0) + 1) -%}
Event #{{ locals.get('n') }} at {{ timestamp.isoformat() }}

Monotonic record ID with shared — a common pattern where all templates in a generator share a single incrementing counter:

{%- set record_id = shared.get('record_id', 1) -%}
... use record_id in the event body ...
{%- do shared.set('record_id', record_id + 1) -%}

Atomic compound operation with globals:

{%- do globals.acquire() -%}
{%- set total = globals.get('total', 0) -%}
{%- do globals.set('total', total + 1) -%}
{%- do globals.release() -%}

Subprocess

The subprocess variable lets you execute shell commands from templates.

ParameterTypeDefaultDescription
commandstringShell command to execute.
cwdstringNoneWorking directory.
envmappingNoneEnvironment variables.
timeoutfloatNoneTimeout in seconds.

The run method returns a result object with three fields:

FieldTypeDescription
stdoutstringStandard output (decoded as UTF-8).
stderrstringStandard error (decoded as UTF-8).
exit_codeintProcess exit code.
{%- set result = subprocess.run('hostname', timeout=5.0) -%}
{{ result.stdout | trim }}

Subprocess calls run synchronously and block event production. Use short timeouts to avoid stalling the pipeline.

On this page