pythonsidecar

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package pythonsidecar provides a mechanism to manage the lifecycle of a Python process as a sidecar component in a Go application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Setup

func Setup(ctx context.Context, opts SetupOptions) (string, error)

Setup extracts the embedded filesystem assets to TargetDir. It returns TargetDir, which can be set as PythonPath in Config.

func TCPReady

func TCPReady(addr string) func(ctx context.Context) error

TCPReady returns a ReadyFunc that attempts to establish a TCP connection to addr (e.g., "127.0.0.1:50053").

Types

type Config

type Config struct {
	// Module is the Python module name to run using the "-m" flag. (Required)
	Module string
	// Args contains any additional arguments to pass to the module. (Optional)
	Args []string
	// Stdout redirects the sidecar's standard output. (Optional)
	Stdout io.Writer
	// Stderr redirects the sidecar's standard error. (Optional)
	Stderr io.Writer
	// ReadyFunc is an optional function to check if the server is ready to accept requests.
	// When provided, Start will poll ReadyFunc until it returns nil or the context expires. (Optional)
	ReadyFunc func(ctx context.Context) error
}

Config holds the configuration parameters for the sidecar lifecycle.

type SetupOptions

type SetupOptions struct {
	// FS is the embedded filesystem containing Python assets (e.g., python.FS). (Required)
	FS fs.FS
}

SetupOptions configures asset extraction and environment setup for a Python sidecar.

type Sidecar

type Sidecar struct {
	// contains filtered or unexported fields
}

Sidecar manages the lifecycle of the underlying Python process.

func New

func New(cfg Config) *Sidecar

New creates a new Sidecar instance using the provided configuration struct.

func (*Sidecar) IsRunning

func (s *Sidecar) IsRunning() bool

IsRunning returns true if the Python process is currently active.

func (*Sidecar) Pid

func (s *Sidecar) Pid() int

Pid returns the process ID of the running sidecar, or 0 if not running.

func (*Sidecar) Start

func (s *Sidecar) Start(ctx context.Context, pythonPath string) error

Start launches the Python process and monitors its lifecycle in the background. If ReadyFunc is configured, Start blocks until the server is ready or the context expires. If the process fails to start or become ready, an error is returned immediately.

func (*Sidecar) Stop

func (s *Sidecar) Stop() error

Stop gracefully terminates the Python process using SIGTERM, falling back to SIGKILL if necessary.

func (*Sidecar) Wait

func (s *Sidecar) Wait() error

Wait blocks until the sidecar exits or crashes, returning the exit error if any.

func (*Sidecar) WaitUntilReady

func (s *Sidecar) WaitUntilReady(ctx context.Context) error

WaitUntilReady blocks until ReadyFunc returns nil, the context is canceled, or the process exits prematurely. If no ReadyFunc is configured in Config, this method returns nil immediately.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL