Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ evaluate expressions, and stream from generators - all without blocking Erlang
schedulers.

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

Expand All @@ -38,7 +38,8 @@ Key features:
## Requirements

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

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

%% Call from Python - native import syntax (recommended)
{ok, Result} = py:exec(<<"
from erlang import my_func
result = my_func(10, 20)
">>).
%% Result = 30
ok = py:exec(<<"from erlang import my_func">>).
{ok, 30} = py:eval(<<"my_func(10, 20)">>).

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

# Call from Python - native import syntax
{:ok, 3628800} = :py.exec("""
from erlang import factorial
result = factorial(10)
""")
:ok = :py.exec("from erlang import factorial")
{:ok, 3628800} = :py.eval("factorial(10)")

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

**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).
**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).

**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.

Expand Down
2 changes: 1 addition & 1 deletion docs/isolated.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Options of `py_context:new/1` specific to this mode:

| Option | Default | Meaning |
|---|---|---|
| `python` | interpreter matching the embedded runtime (`py:python_executable/0`), or `isolated_python` app env | Executable to run |
| `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 |
| `rlimits` | `#{}` | `#{as => Bytes, cpu => Seconds, nofile => N}`, applied with `setrlimit` before any user code |
| `cgroup` | none | Path of a cgroup v2 directory the child joins (limits written by you: `memory.max`, `cpu.max`, `pids.max`) |
| `env` | `#{}` | Extra environment variables for the child |
Expand Down
4 changes: 2 additions & 2 deletions docs/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This guide covers breaking changes and migration steps when upgrading erlang_pyt
### Quick Checklist

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

Expand Down Expand Up @@ -36,7 +36,7 @@ application:set_env(erlang_python, context_mode, owngil).

### Removed Functions

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

<!-- skip-lint -->
```erlang
Expand Down
8 changes: 5 additions & 3 deletions docs/sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ ok = py_session:close(S).
`orders` is imported fresh on the first call and kept for the next ones;
another session has its own copy. `py_context:exec/eval` use the session's
own `__main__`, and `py:call(S, ...)` works too. A callback can call back
into the same session. The modules are freed on `close/1`, when the
process that opened the session crashes, or if the session process is
killed.
into the same session. `py_session:close/1` closes the re-import session
without waiting for Python. If a call is still running, even after its
caller timed out, the shared context finishes it before freeing the
session's modules. Cleanup also runs if the owner crashes or the session
process is killed.

The session runs on one of the template's contexts, shared with other
sessions, so two things differ from a fork session:
Expand Down
2 changes: 1 addition & 1 deletion docs/shared-dict.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,6 @@ cache['computed'] = 42

## See Also

- [State API](state.md) - Global shared state (different scope)
- [Shared state between workers](../README.md#shared-state-between-workers) - Access values by key across workers
- [Channel](channel.md) - Message passing between Erlang and Python
- [Getting Started](getting-started.md) - Basic usage guide
6 changes: 3 additions & 3 deletions docs/workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ ok = py_context:start_loop(W). %% same context, fresh loop
the loop is stopped.
- Put workers under your own supervisor. `py_context` processes are
`temporary` under `py_context_sup`; a restart is `py_context:new/1` again,
`start_loop`, and a new dup of the listen fd. Use `memory_limit` in
`py_context:new/1` to cap a worker, and `py_nif:context_memory_usage/1` to
watch it.
`start_loop`, and a new dup of the listen fd. For an `owngil` worker,
see [Per-context memory caps](memory.md#per-context-memory-caps) for how
to enable memory limits, set a cap in bytes and read current usage.

## Rules

Expand Down
Loading