HOPS
HOPS class reference
Classes | Static Public Member Functions | List of all members
hops::MHO_PythonSubprocessRunner Class Reference

Pure-C++ (no pybind, no libpython) helper that runs the user's python3 and exchanges JSON with it. This is the interprocess comms primitive used by the no-embed subprocess backends (control-file evaluation and default plotting). One process is spawned per action (no persistent worker!), which is slow and a lot of overhead, but this is ok for a fallback. More...

#include <MHO_PythonSubprocessRunner.hh>

Classes

struct  Result
 

Static Public Member Functions

static std::string ResolveInterpreter ()
 Resolve the python interpreter to use: $HOPS_PYTHON, else "python3" (left for PATH resolution by the shell). More...
 
static std::string ResolvePackageDir ()
 Install-prefix-relative directory holding the shipped hops_* python packages, resolved at runtime against GetHopsInstallPrefix() (or $HOPS_INSTALL). Empty if it cannot be determined. More...
 
static Result RunModule (const std::string &module, const std::string &request_json)
 Run python -m <module> <request_file>, feeding the JSON request_json via a temp file, and capture stdout/stderr. The child's PYTHONPATH is prepended with ResolvePackageDir() so the shipped hops packages import cleanly. More...
 

Detailed Description

Pure-C++ (no pybind, no libpython) helper that runs the user's python3 and exchanges JSON with it. This is the interprocess comms primitive used by the no-embed subprocess backends (control-file evaluation and default plotting). One process is spawned per action (no persistent worker!), which is slow and a lot of overhead, but this is ok for a fallback.

Author
J. Barrett - barre.nosp@m.ttj@.nosp@m.mit.e.nosp@m.du

Implementation note: we use popen() (a unidirectional pipe) and pass the JSON request as a temp file given on the child's command line, reading the JSON response back off the child's stdout. This sidesteps the bidirectional-pipe deadlock concerns of a hand-rolled fork/exec while keeping the code small.

Interpreter discovery: $HOPS_PYTHON if set, else "python3" on PATH. Package discovery: the shipped pure-python hops_* packages are located via MHO_DirectoryInterface::GetHopsInstallPrefix() + the install-relative site subdir (baked in as HOPS_PYTHON_SITE_SUBDIR, matching hops_pypath.sh.in), and prepended to the child's PYTHONPATH so the default control/plot entry points are self-contained (no user pip install required).

Member Function Documentation

◆ ResolveInterpreter()

std::string hops::MHO_PythonSubprocessRunner::ResolveInterpreter ( )
static

Resolve the python interpreter to use: $HOPS_PYTHON, else "python3" (left for PATH resolution by the shell).

◆ ResolvePackageDir()

std::string hops::MHO_PythonSubprocessRunner::ResolvePackageDir ( )
static

Install-prefix-relative directory holding the shipped hops_* python packages, resolved at runtime against GetHopsInstallPrefix() (or $HOPS_INSTALL). Empty if it cannot be determined.

◆ RunModule()

MHO_PythonSubprocessRunner::Result hops::MHO_PythonSubprocessRunner::RunModule ( const std::string &  module,
const std::string &  request_json 
)
static

Run python -m <module> <request_file>, feeding the JSON request_json via a temp file, and capture stdout/stderr. The child's PYTHONPATH is prepended with ResolvePackageDir() so the shipped hops packages import cleanly.

Parameters
modulepython module to run with -m (e.g. "hops_control").
request_jsonthe JSON request payload written to the temp file.
Returns
Result with capture + exit status. spawned==false means the interpreter could not be launched at all.

The documentation for this class was generated from the following files: