Documentation
¶
Overview ¶
Package serpent provides functions for interacting with a Python interpreter.
Index ¶
- Variables
- func Close() error
- func Init(libraryPath string) error
- func InitSingleWorker(libraryPath string) error
- func Lib() (string, error)
- func Run[TInput, TResult any](program Program[TInput, TResult], arg TInput) (TResult, error)
- func RunWrite[TInput any](w io.Writer, program Program[TInput, Writer], arg TInput) error
- type Executable
- type Program
- type PythonNotInitialized
- type Writer
- type WriterExecutable
Constants ¶
This section is empty.
Variables ¶
var ( // ErrAlreadyInitialized is returned when the Python interpreter is initialized more than once. ErrAlreadyInitialized = errors.New("already initialized") // ErrRunFailed is returned when the Python program fails to run. ErrRunFailed = errors.New("run failed") // ErrSubInterpreterFailed is returned when a sub-interpreter fails to initialize. ErrSubInterpreterFailed = errors.New("sub-interpreter creation failed") // ErrNoHealthyWorkers is returned when all workers have failed. ErrNoHealthyWorkers = errors.New("no healthy workers available") // ErrNotInitialized is returned when Close is called before Init. ErrNotInitialized = errors.New("not initialized") )
var ErrLibraryNotFound = errors.New("library not found")
ErrLibraryNotFound is returned when the Python shared library cannot be found.
Functions ¶
func Init ¶
Init initializes the Python interpreter with runtime.NumCPU() workers. This must be called before any other functions in this package. When using packages that are incompatible with sub-interpreters, use InitSingleWorker instead.
func InitSingleWorker ¶
InitSingleWorker initializes the Python interpreter with a single worker, disabling sub-interpreters. Use this when running Python code that uses C extension modules incompatible with sub-interpreters. This must be called before any other functions in this package. Use Init for normal usage.
func Lib ¶
Lib attempts to find a Python shared library on the system and returns the path if found. If the library cannot be found, ErrLibraryNotFound is returned. If the LIBPYTHON_PATH envrionment variable is set, the value of that environment variable is returned.
func Run ¶
Run runs a Program with the supplied argument and returns the result. The Python code must define a run() function that accepts the input and returns a JSON-serializable value.
Example Python program:
def run(input):
return input + 1
Types ¶
type Executable ¶
type Executable[TInput, TResult any] struct { // contains filtered or unexported fields }
Executable represents a loaded Python program that can be called multiple times. It is pinned to a single worker on first Run(), preserving module-level state across calls. An Executable is not safe for concurrent use; create a separate instance for each goroutine.
func Load ¶
func Load[TInput, TResult any](program Program[TInput, TResult]) (*Executable[TInput, TResult], error)
Load loads a Python program and returns an Executable that can be called multiple times. The executable is pinned to a worker on first Run(), and all subsequent calls use the same worker.
func (*Executable) Close ¶
func (b *Executable) Close() error
Close releases resources associated with the executable.
func (*Executable[TInput, TResult]) Run ¶
func (e *Executable[TInput, TResult]) Run(arg TInput) (TResult, error)
Run executes the loaded program with the given input. On first call, the program is loaded on a worker and pinned to it. Subsequent calls reuse the same worker and loaded state.
type PythonNotInitialized ¶
type PythonNotInitialized string
PythonNotInitialized is a panic type indicating that the Python interpreter has not been initialized.
type Writer ¶
type Writer struct{}
Writer is a result type which indicates that the program writes to the output. e.g. Program[string, Writer] is a program that writes to the output.
type WriterExecutable ¶
type WriterExecutable[TInput any] struct { // contains filtered or unexported fields }
WriterExecutable represents a loaded Python program that writes to an output stream. A WriterExecutable is not safe for concurrent use; create a separate instance for each goroutine.
func LoadWriter ¶
func LoadWriter[TInput any](program Program[TInput, Writer]) (*WriterExecutable[TInput], error)
LoadWriter loads a Python program that writes to an output stream.