Process Module API Reference¶
This section documents the internals of the process
module in Bijux CLI.
bijux_cli.infra.process ¶
Provides a process pool service for executing external commands.
This module defines the ProcessPool
class, a concrete implementation of the ProcessPoolProtocol
. It is designed to run shell commands in isolated subprocesses using a managed pool of workers. Key features include command validation to prevent shell injection and an in-memory LRU cache to return results for repeated commands without re-execution.
ProcessPool ¶
ProcessPool(
observability: ObservabilityProtocol,
telemetry: TelemetryProtocol,
max_workers: int = 4,
)
Bases: ProcessPoolProtocol
Executes validated commands in a worker pool with an LRU cache.
This class manages a ProcessPoolExecutor
to run commands in separate processes. It maintains a cache of recent command results to avoid unnecessary re-execution.
Attributes:
-
_MAX_CACHE
(int
) –The maximum number of command results to store in the LRU cache.
-
_exec
(ProcessPoolExecutor
) –The underlying executor for running tasks.
-
_log
(ObservabilityProtocol
) –The logging service.
-
_tel
(TelemetryProtocol
) –The telemetry service.
-
_cache
(OrderedDict
) –An LRU cache storing tuples of command arguments to their results
(returncode, stdout, stderr)
.
Initializes the ProcessPool
service.
Parameters:
-
observability
(ObservabilityProtocol
) –The service for logging.
-
telemetry
(TelemetryProtocol
) –The service for event tracking.
-
max_workers
(int
, default:4
) –The maximum number of worker processes. This can be overridden by the
BIJUXCLI_MAX_WORKERS
environment variable.
Source code in src/bijux_cli/infra/process.py
get_status ¶
Returns the current status of the process pool.
Returns:
-
dict[str, Any]
–dict[str, Any]: A dictionary containing status information, such as the number of commands processed (and cached).
Source code in src/bijux_cli/infra/process.py
run ¶
Executes a command in the process pool, using a cache.
The command is first looked up in the LRU cache. If not found, it is validated and then executed in a subprocess. The result is then stored in the cache.
Parameters:
-
cmd
(list[str]
) –The command and its arguments to execute.
-
executor
(str
) –The name of the entity requesting the execution, used for telemetry.
Returns:
-
tuple[int, bytes, bytes]
–tuple[int, bytes, bytes]: A tuple containing the command's return code, standard output, and standard error.
Raises:
-
BijuxError
–If command validation fails or if an unexpected error occurs during subprocess execution.
Source code in src/bijux_cli/infra/process.py
shutdown ¶
Gracefully shuts down the worker process pool.
get_process_pool ¶
get_process_pool(
logger: ObservabilityProtocol,
telemetry: TelemetryProtocol,
) -> ProcessPoolProtocol
A factory function for creating a ProcessPool
instance.
This helper respects the BIJUXCLI_MAX_WORKERS
environment variable to configure the number of worker processes.
Parameters:
-
logger
(ObservabilityProtocol
) –The logging service instance.
-
telemetry
(TelemetryProtocol
) –The telemetry service instance.
Returns:
-
ProcessPoolProtocol
(ProcessPoolProtocol
) –A configured instance of the process pool service.