|
| 1 | +#!/usr/bin/env escript |
| 2 | +%%% @doc Shared state example - demonstrates sharing data between Python workers. |
| 3 | +%%% |
| 4 | +%%% Since each Python worker has its own namespace, functions and variables |
| 5 | +%%% defined in one call aren't visible in another. The shared state API |
| 6 | +%%% provides ETS-backed storage accessible from both Python and Erlang. |
| 7 | +%%% |
| 8 | +%%% Prerequisites: rebar3 compile |
| 9 | +%%% Run from project root: escript examples/shared_state_example.erl |
| 10 | + |
| 11 | +-mode(compile). |
| 12 | + |
| 13 | +main(_) -> |
| 14 | + %% Add the compiled beam files to the code path |
| 15 | + ScriptDir = filename:dirname(escript:script_name()), |
| 16 | + ProjectRoot = filename:dirname(ScriptDir), |
| 17 | + EbinDir = filename:join([ProjectRoot, "_build", "default", "lib", "erlang_python", "ebin"]), |
| 18 | + true = code:add_pathz(EbinDir), |
| 19 | + |
| 20 | + {ok, _} = application:ensure_all_started(erlang_python), |
| 21 | + |
| 22 | + io:format("~n=== Shared State Example ===~n~n"), |
| 23 | + |
| 24 | + %% Clear any existing state |
| 25 | + py:state_clear(), |
| 26 | + |
| 27 | + %% Example 1: Set from Erlang, read from Python |
| 28 | + io:format("1. Erlang -> Python:~n"), |
| 29 | + py:state_store(<<"config">>, #{ |
| 30 | + api_key => <<"secret123">>, |
| 31 | + max_retries => 3, |
| 32 | + timeout => 5000 |
| 33 | + }), |
| 34 | + %% Python reads using idiomatic import syntax |
| 35 | + ok = py:exec(<<" |
| 36 | +from erlang import state_get |
| 37 | +
|
| 38 | +config = state_get('config') |
| 39 | +assert config['api_key'] == 'secret123' |
| 40 | +assert config['max_retries'] == 3 |
| 41 | +">>), |
| 42 | + io:format(" Python read config successfully~n"), |
| 43 | + |
| 44 | + %% Example 2: Set from Python, read from Erlang |
| 45 | + io:format("~n2. Python -> Erlang:~n"), |
| 46 | + ok = py:exec(<<" |
| 47 | +from erlang import state_set |
| 48 | +
|
| 49 | +state_set('computation_result', { |
| 50 | + 'status': 'complete', |
| 51 | + 'values': [1, 2, 3, 4, 5], |
| 52 | + 'metadata': {'source': 'python', 'timestamp': 1234567890} |
| 53 | +}) |
| 54 | +">>), |
| 55 | + {ok, Result} = py:state_fetch(<<"computation_result">>), |
| 56 | + io:format(" Result in Erlang: ~p~n", [Result]), |
| 57 | + |
| 58 | + %% Example 3: Atomic counters - thread-safe increment/decrement |
| 59 | + io:format("~n3. Atomic counters (thread-safe):~n"), |
| 60 | + |
| 61 | + %% From Erlang |
| 62 | + Val1 = py:state_incr(<<"hits">>), |
| 63 | + io:format(" Erlang incr: ~p~n", [Val1]), |
| 64 | + Val2 = py:state_incr(<<"hits">>, 10), |
| 65 | + io:format(" Erlang incr by 10: ~p~n", [Val2]), |
| 66 | + |
| 67 | + %% From Python |
| 68 | + ok = py:exec(<<" |
| 69 | +from erlang import state_incr, state_decr |
| 70 | +
|
| 71 | +val = state_incr('hits', 5) |
| 72 | +print(f' Python incr by 5: {val}') |
| 73 | +
|
| 74 | +val = state_decr('hits', 3) |
| 75 | +print(f' Python decr by 3: {val}') |
| 76 | +">>), |
| 77 | + |
| 78 | + {ok, FinalCount} = py:state_fetch(<<"hits">>), |
| 79 | + io:format(" Final counter value: ~p~n", [FinalCount]), |
| 80 | + |
| 81 | + %% Example 4: List all keys from Python |
| 82 | + io:format("~n4. List keys from Python:~n"), |
| 83 | + ok = py:exec(<<" |
| 84 | +from erlang import state_keys |
| 85 | +
|
| 86 | +keys = state_keys() |
| 87 | +assert 'hits' in keys |
| 88 | +assert 'config' in keys |
| 89 | +">>), |
| 90 | + Keys = py:state_keys(), |
| 91 | + io:format(" Keys: ~p~n", [Keys]), |
| 92 | + |
| 93 | + %% Example 5: Delete from Python |
| 94 | + io:format("~n5. Delete from Python:~n"), |
| 95 | + ok = py:exec(<<" |
| 96 | +from erlang import state_delete |
| 97 | +
|
| 98 | +state_delete('computation_result') |
| 99 | +">>), |
| 100 | + case py:state_fetch(<<"computation_result">>) of |
| 101 | + {error, not_found} -> io:format(" Key deleted successfully~n"); |
| 102 | + {ok, _} -> io:format(" ERROR: Key still exists~n") |
| 103 | + end, |
| 104 | + |
| 105 | + %% Cleanup |
| 106 | + py:state_clear(), |
| 107 | + |
| 108 | + io:format("~n=== Done ===~n~n"), |
| 109 | + ok = application:stop(erlang_python). |
0 commit comments