Skip to content

Commit 5f1bb82

Browse files
committed
Correct callback examples and release guides
Show how to import a callback and read its result from Erlang and Elixir. List the native build prerequisites, explain when re-import sessions release their modules, and point readers to the current guides for shared state and memory caps.
1 parent 44608f7 commit 5f1bb82

6 files changed

Lines changed: 20 additions & 22 deletions

File tree

‎README.md‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ evaluate expressions, and stream from generators - all without blocking Erlang
1616
schedulers.
1717

1818
**Parallelism options:**
19-
- **Worker mode** (default, recommended) - Works with any Python version. With free-threaded Python (3.13t+), provides true parallelism automatically.
19+
- **Worker mode** (default, recommended) - Works with every supported Python version. With free-threaded Python (3.13t+), provides true parallelism automatically.
2020
- **OWN_GIL sub-interpreters** (Python 3.14+) - Each interpreter has its own GIL, true parallelism.
2121
- **BEAM processes** - Fan out work across lightweight Erlang processes.
2222

@@ -38,7 +38,8 @@ Key features:
3838
## Requirements
3939

4040
- Erlang/OTP 28 or later (tested on OTP 28 and 29)
41-
- Python 3.12+ (3.13+ for free-threading)
41+
- CPython 3.12+ with matching development headers (`Python.h`); 3.13+ for free-threading
42+
- rebar3 and CMake 3.18 or later
4243
- C compiler (gcc, clang)
4344

4445
## Building
@@ -112,11 +113,8 @@ Register Erlang or Elixir functions that Python code can call back into:
112113
py:register_function(my_func, fun([X, Y]) -> X + Y end).
113114

114115
%% Call from Python - native import syntax (recommended)
115-
{ok, Result} = py:exec(<<"
116-
from erlang import my_func
117-
result = my_func(10, 20)
118-
">>).
119-
%% Result = 30
116+
ok = py:exec(<<"from erlang import my_func">>).
117+
{ok, 30} = py:eval(<<"my_func(10, 20)">>).
120118

121119
%% Or use attribute-style access
122120
{ok, 30} = py:eval(<<"erlang.my_func(10, 20)">>).
@@ -137,10 +135,8 @@ py:unregister_function(my_func).
137135
end)
138136

139137
# Call from Python - native import syntax
140-
{:ok, 3628800} = :py.exec("""
141-
from erlang import factorial
142-
result = factorial(10)
143-
""")
138+
:ok = :py.exec("from erlang import factorial")
139+
{:ok, 3628800} = :py.eval("factorial(10)")
144140

145141
# Or use attribute-style access
146142
{:ok, 3628800} = :py.eval("erlang.factorial(10)")
@@ -632,7 +628,7 @@ Bulk data crosses through shared memory (`py_shm`, with the optional
632628
[iommap](https://hex.pm/packages/iommap) dependency). See
633629
[Isolated Contexts](docs/isolated.md).
634630

635-
**Worker mode is recommended** because it works with any Python version and automatically benefits from free-threaded Python (3.13t+) when available. Each context owns a dedicated pthread, providing stable thread affinity for libraries with thread-local state (numpy, torch, tensorflow).
631+
**Worker mode is recommended** because it works with every supported Python version and automatically benefits from free-threaded Python (3.13t+) when available. Each context owns a dedicated pthread, providing stable thread affinity for libraries with thread-local state (numpy, torch, tensorflow).
636632

637633
**Why OWN_GIL requires Python 3.14+**: Some C extensions (e.g., `_decimal`, `numpy`) have global state bugs in sub-interpreters on Python 3.12/3.13. These are fixed in Python 3.14.
638634

‎docs/isolated.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Options of `py_context:new/1` specific to this mode:
4949

5050
| Option | Default | Meaning |
5151
|---|---|---|
52-
| `python` | interpreter matching the embedded runtime (`py:python_executable/0`), or `isolated_python` app env | Executable to run |
52+
| `python` | `isolated_python` application setting if set, otherwise the interpreter matching the embedded runtime (falling back to `python3` on `PATH`) | Executable for the child process |
5353
| `rlimits` | `#{}` | `#{as => Bytes, cpu => Seconds, nofile => N}`, applied with `setrlimit` before any user code |
5454
| `cgroup` | none | Path of a cgroup v2 directory the child joins (limits written by you: `memory.max`, `cpu.max`, `pids.max`) |
5555
| `env` | `#{}` | Extra environment variables for the child |

‎docs/migration.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This guide covers breaking changes and migration steps when upgrading erlang_pyt
77
### Quick Checklist
88

99
- [ ] Update `py:execution_mode/0` usage - now returns `worker | owngil` only
10-
- [ ] Remove any `py:num_executors/0` calls (function removed)
10+
- [ ] Remove calls to the former executor count function, `py:num_executors()`
1111
- [ ] Update code that checks for `free_threaded` or `multi_executor` modes
1212
- [ ] Review `context_mode` configuration (now `worker | owngil`)
1313

@@ -36,7 +36,7 @@ application:set_env(erlang_python, context_mode, owngil).
3636

3737
### Removed Functions
3838

39-
**`py:num_executors/0`** - Removed. Contexts now use per-context worker threads.
39+
Each context now has its own worker thread. Remove calls to `py:num_executors()`.
4040

4141
<!-- skip-lint -->
4242
```erlang

‎docs/sessions.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ ok = py_session:close(S).
112112
`orders` is imported fresh on the first call and kept for the next ones;
113113
another session has its own copy. `py_context:exec/eval` use the session's
114114
own `__main__`, and `py:call(S, ...)` works too. A callback can call back
115-
into the same session. The modules are freed on `close/1`, when the
116-
process that opened the session crashes, or if the session process is
117-
killed.
115+
into the same session. `py_session:close/1` closes the re-import session
116+
without waiting for Python. If a call is still running, even after its
117+
caller timed out, the shared context finishes it before freeing the
118+
session's modules. Cleanup also runs if the owner crashes or the session
119+
process is killed.
118120

119121
The session runs on one of the template's contexts, shared with other
120122
sessions, so two things differ from a fork session:

‎docs/shared-dict.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,6 @@ cache['computed'] = 42
298298

299299
## See Also
300300

301-
- [State API](state.md) - Global shared state (different scope)
301+
- [Shared state between workers](../README.md#shared-state-between-workers) - Access values by key across workers
302302
- [Channel](channel.md) - Message passing between Erlang and Python
303303
- [Getting Started](getting-started.md) - Basic usage guide

‎docs/workers.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ ok = py_context:start_loop(W). %% same context, fresh loop
157157
the loop is stopped.
158158
- Put workers under your own supervisor. `py_context` processes are
159159
`temporary` under `py_context_sup`; a restart is `py_context:new/1` again,
160-
`start_loop`, and a new dup of the listen fd. Use `memory_limit` in
161-
`py_context:new/1` to cap a worker, and `py_nif:context_memory_usage/1` to
162-
watch it.
160+
`start_loop`, and a new dup of the listen fd. For an `owngil` worker,
161+
see [Per-context memory caps](memory.md#per-context-memory-caps) for how
162+
to enable memory limits, set a cap in bytes and read current usage.
163163

164164
## Rules
165165

0 commit comments

Comments
 (0)