py_state (erlang_python v3.0.0)

View Source

Shared 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

clear()

-spec clear() -> ok.

Clear all entries from the shared state.

decr(Key)

-spec decr(Key :: term()) -> integer().

Atomically decrement a counter by 1.

decr(Key, Amount)

-spec decr(Key :: term(), Amount :: integer()) -> integer().

Atomically decrement a counter by Amount.

fetch(Key)

-spec fetch(Key :: term()) -> {ok, term()} | {error, not_found}.

Fetch a value from the shared state.

incr(Key)

-spec incr(Key :: term()) -> integer().

Atomically increment a counter by 1. Initializes to 1 if not exists.

incr(Key, Amount)

-spec incr(Key :: term(), Amount :: integer()) -> integer().

Atomically increment a counter by Amount. Initializes to Amount if not exists.

init_tab()

-spec init_tab() -> ok.

Initialize the ETS table for shared state. Called by supervisor for resilience - table survives process crashes.

keys()

-spec keys() -> [term()].

Get all keys in the shared state.

register_callbacks()

-spec register_callbacks() -> ok.

Register state functions as callbacks for Python access. Called after py_callback is started.

remove(Key)

-spec remove(Key :: term()) -> ok.

Remove a key from the shared state.

store(Key, Value)

-spec store(Key :: term(), Value :: term()) -> ok.

Store a value in the shared state.