runner

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package runner 提供 Python 子进程运行器。 管理 Python 进程的启动、stdin/stdout JSON lines 通信和生命周期。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertEnv

func ConvertEnv(raw map[string]interface{}) map[string]string

ConvertEnv 将 map[string]interface{} 转为 map[string]string。 用于兼容 YAML 中非字符串类型的环境变量值(如 WORKER_COUNT: 8)。

func DefaultStderrHandler

func DefaultStderrHandler(prefix string) func(string)

DefaultStderrHandler 返回一个默认的 stderr 日志处理器。 自动解析 Python logging 的级别前缀(如 INFO:、WARNING:、ERROR:), 并使用对应的 Go 日志级别输出。未识别的前缀默认为 WARN。 prefix 为日志来源标识,如 "[python-processor]"。

Types

type Config

type Config struct {
	// Script Python 脚本路径(必填)
	Script string `json:"script"`
	// PythonExec Python 可执行文件路径,默认 "python3"
	PythonExec string `json:"python_exec"`
	// Args 传递给脚本的额外命令行参数
	Args []string `json:"args"`
	// Env 额外的环境变量(会合并到进程环境中)
	Env map[string]string `json:"env"`
}

Config Runner 的配置。

type Runner

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

Runner 管理 Python 子进程的生命周期和通信。

func New

func New(cfg Config) (*Runner, error)

New 创建 Runner 实例。 解析配置,转绝对路径,验证脚本存在。

func (*Runner) Close

func (r *Runner) Close() error

Close 关闭 stdin 并等待进程退出。 如果进程超时未退出则发送 SIGKILL。 幂等操作,多次调用安全。

func (*Runner) CloseStdin

func (r *Runner) CloseStdin() error

CloseStdin 关闭 Python 进程的 stdin,通知 Python 输入结束。 不等待进程退出,由 Python 自行处理剩余数据后退出。

func (*Runner) ReadLine

func (r *Runner) ReadLine(ctx context.Context) ([]byte, error)

ReadLine 从 Python stdout 读取一行数据。 返回 io.EOF 表示 Python 进程已退出(stdout 关闭)。

func (*Runner) SetStderrCallback

func (r *Runner) SetStderrCallback(fn func(line string))

SetStderrCallback 设置 stderr 日志回调函数。 每当 Python 进程输出一行 stderr 时调用。

func (*Runner) Start

func (r *Runner) Start(ctx context.Context) error

Start 启动 Python 子进程,建立 stdin/stdout/stderr 管道。 启动后 stdout 由专用协程持续读取并通过 ReadLine 获取。

func (*Runner) WriteLine

func (r *Runner) WriteLine(ctx context.Context, line []byte) error

WriteLine 向 Python stdin 写入一行数据。

Jump to

Keyboard shortcuts

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