A live Python interpreter: a namespace plus the thread that runs it.
A session is a plain struct wrapping a NIF resource, so it can be passed between processes and stored in any state. Requests from different processes are queued and run one at a time, and each answer goes back to the process that asked for it.
The interpreter thread stops when the session is closed with close/1 or
when the struct becomes garbage — hold on to it for as long as you need the
Python state to survive.
{:ok, session} = Rupyex.Session.open()
{:ok, 3} = Rupyex.eval(session, "1 + 2")
:ok = Rupyex.Session.close(session)
Summary
Types
@type open_option() :: {:stdlib, boolean()} | {:capture_output, boolean()} | {:sys_path, [String.t()]} | {:argv, [String.t()]} | {:init, String.t() | nil} | {:timeout, timeout()} | {:open_timeout, timeout()}
Options for open/1:
:stdlib— make the embedded Python standard library importable (defaulttrue):capture_output— sendsys.stdout/sys.stderrtoRupyex.Resultinstead of the OS streams (defaulttrue); withfalse, Python prints land on the BEAM's own stdout:sys_path— extrasys.pathentries, for importing real.pyfiles:argv—sys.argv:init— Python source to run once at start-up:timeout— default timeout for every call on this session (default5000ms):open_timeout— how long to wait for the interpreter to start (default30000ms)
Functions
@spec close(t()) :: :ok
Stop the interpreter and release everything it holds.
Any queued request fails with a :closed error. Closing twice is fine.
@spec interrupt(t()) :: :ok
Abort whatever the session is running right now.
The running Python code receives a KeyboardInterrupt at its next safe
point, which it may catch. Callers waiting on that job get an
:interrupted error.
@spec open([open_option()]) :: {:ok, t()} | {:error, Rupyex.Error.t()}
Start an interpreter.
Blocks until the interpreter is ready, so start-up failures (a broken
:init script, for instance) are reported here rather than on first use.
@spec open!([open_option()]) :: t()
Same as open/1, but raises on failure.
Whether the session still accepts requests.