py_state (erlang_python v3.0.0)
View SourceShared state storage for Python workers.
This module provides a simple key-value store backed by ETS that Python code can use to share state between workers. Since each Python worker has its own namespace, this provides a way to share data across calls.
Python Usage
python
from erlang import state_set, state_get, state_delete, state_keys
from erlang import state_incr, state_decr
# Store data
state_set('my_key', {'data': [1, 2, 3]})
# Retrieve data
value = state_get('my_key') # {'data': [1, 2, 3]}
# Atomic counters (thread-safe)
state_incr('hits') # increment by 1, returns new value
state_incr('hits', 10) # increment by 10
state_decr('hits') # decrement by 1
state_decr('hits', 5) # decrement by 5
# Delete data
state_delete('my_key')
# List all keys
keys = state_keys() # ['other_key', ...]Erlang Usage
erlang
py_state:store(<<"my_key">>, #{data => [1, 2, 3]}).
{ok, Value} = py_state:fetch(<<"my_key">>).
%% Atomic counters
1 = py_state:incr(<<"counter">>).
11 = py_state:incr(<<"counter">>, 10).
10 = py_state:decr(<<"counter">>).
Summary
Functions
Clear all entries from the shared state.
Atomically decrement a counter by 1.
Atomically decrement a counter by Amount.
Fetch a value from the shared state.
Atomically increment a counter by 1. Initializes to 1 if not exists.
Atomically increment a counter by Amount. Initializes to Amount if not exists.
Initialize the ETS table for shared state. Called by supervisor for resilience - table survives process crashes.
Get all keys in the shared state.
Register state functions as callbacks for Python access. Called after py_callback is started.
Remove a key from the shared state.
Store a value in the shared state.
Functions
-spec clear() -> ok.
Clear all entries from the shared state.
Atomically decrement a counter by 1.
Atomically decrement a counter by Amount.
Fetch a value from the shared state.
Atomically increment a counter by 1. Initializes to 1 if not exists.
Atomically increment a counter by Amount. Initializes to Amount if not exists.
-spec init_tab() -> ok.
Initialize the ETS table for shared state. Called by supervisor for resilience - table survives process crashes.
-spec keys() -> [term()].
Get all keys in the shared state.
-spec register_callbacks() -> ok.
Register state functions as callbacks for Python access. Called after py_callback is started.
-spec remove(Key :: term()) -> ok.
Remove a key from the shared state.
Store a value in the shared state.