Snex.Interpreter (Snex v0.2.0)

View Source

Runs a Python interpreter in a separate OS process.

This module is responsible for facilitating in-and-out communication between Elixir and the spawned Python interpreter.

Usually you won't interact with this module directly. Instead, you would create a custom interpreter module with use Snex.Interpreter:

defmodule SnexTest.NumpyInterpreter do
  use Snex.Interpreter,
    pyproject_toml: """
    [project]
    name = "my-numpy-project"
    version = "0.0.0"
    requires-python = "==3.11.*"
    dependencies = ["numpy>=2"]
    """
  end

See the Snex module documentation for more detail.

Summary

Types

Options for start_link/1.

Running instance of Snex.Interpreter.

Functions

Returns a specification to start this module under a supervisor.

Starts a new Python interpreter.

Types

option()

@type option() ::
  {:python, String.t()}
  | {:cd, Path.t()}
  | {:environment, %{optional(String.t()) => String.t()}}
  | {:init_script, String.t()}
  | {:sync_start?, boolean()}
  | GenServer.option()

Options for start_link/1.

server()

@type server() :: GenServer.server()

Running instance of Snex.Interpreter.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

start_link(opts \\ [])

@spec start_link([option()]) :: GenServer.on_start()

Starts a new Python interpreter.

The interpreter can be used by functions in the Snex module.

Options

  • :python - The Python executable to use. This can be a full path or a command to find via System.find_executable/1.
  • :cd - The directory to change to before running the interpreter.
  • :environment - A map of environment variables to set when running the Python executable.
  • :init_script - A string of Python code to run when the interpreter is started. Failing to run the script will cause the process initialization to fail. The variable context left by the script will be the initial context for all Snex.make_env/3 calls using this interpreter.
  • :sync_start? - If true, the interpreter will start and run the init script in the init callback. Setting this to false is useful for long-running init scripts; the downside is that if something goes wrong, the interpreter process will start crashing after successfully starting as a part of the supervision tree. Default: true.
  • any other options will be passed to GenServer.start_link/3.