# subprocess

Python API: `modelconverter.utils.subprocess`

Execution of external commands used by the conversions.

The conversions drive vendor toolchains through their command line tools. The helpers here run such a command, stream its output
to the log with ANSI escape sequences removed, and record the peak memory usage and the wall-clock run time of the whole process
tree.

## Classes

| Name | Description |
| --- | --- |
| [`SubprocessHandle`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Classes-SubprocessHandle) | Context manager wrapping a subprocess with live psutil access and deferred result collection. |
| [`SubprocessResult`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Classes-SubprocessResult) | Extension of `subprocess.CompletedProcess` that also carries peak memory usage. |

### class `SubprocessHandle`

```python
class SubprocessHandle
```

Context manager wrapping a subprocess with live psutil access and deferred result collection.

Properties

| Name | Type | Description |
| --- | --- | --- |
| [`proc`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Classes-SubprocessHandle-proc) | `subprocess.Popen[str]` | The underlying `subprocess.Popen` object. |
| [`ps_proc`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Classes-SubprocessHandle-ps_proc) | `psutil.Process` | The `psutil.Process` wrapping the process. |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Classes-SubprocessHandle-__init__) | Initialize the subprocess handle. |
| [`is_suspended`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Classes-SubprocessHandle-is_suspended) | Return whether the process is currently suspended. |
| [`poll`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Classes-SubprocessHandle-poll) | Check whether the process has terminated. |
| [`result`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Classes-SubprocessHandle-result) | Collect the result of the finished process. |
| [`resume`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Classes-SubprocessHandle-resume) | Resume the process. |
| [`suspend`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Classes-SubprocessHandle-suspend) | Suspend the process. |
| [`wait`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Classes-SubprocessHandle-wait) | Wait for the process to terminate. |

#### property `proc`

```python
proc: subprocess.Popen[str]
```

The underlying `subprocess.Popen` object.

Raises

| Type | Description |
| --- | --- |
| `RuntimeError` | If the process has not been started yet. |

#### property `ps_proc`

```python
ps_proc: psutil.Process
```

The `psutil.Process` wrapping the process.

Raises

| Type | Description |
| --- | --- |
| `RuntimeError` | If the process has not been started yet. |

#### method `__init__`

```python
def __init__(
    cmd: str | Sequence[PathType],
    *,
    silent: bool = False,
    timeout: float | None = None,
)
```

Initialize the subprocess handle.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `cmd` | `str | Sequence[PathType]` | Command to execute. If a string is given, it will be split on whitespace. If a list is given, each element will be converted to a string. | required |
| `silent` | `bool` | If `True`, suppress all output from the command. | `False` |
| `timeout` | `float | None` | If given, the maximum time in seconds to allow the process to run. An exceeded timeout raises `subprocess.TimeoutExpired` -- from a truth test, which also terminates the process, or from `wait`, which leaves it running. | `None` |

#### method `is_suspended`

```python
def is_suspended() -> bool
```

Return whether the process is currently suspended.

#### method `poll`

```python
def poll() -> int | None
```

Check whether the process has terminated.

Returns

| Type | Description |
| --- | --- |
| `int | None` | The return code, or `None` if the process is still running. |

#### method `result`

```python
def result() -> SubprocessResult
```

Collect the result of the finished process.

Waits for the reader threads to finish and logs a summary of the run unless the handle is silent.

Returns

| Type | Description |
| --- | --- |
| `SubprocessResult` | Result of the command, carrying its captured output, the peak memory usage and the total run time. |

Raises

| Type | Description |
| --- | --- |
| `subprocess.SubprocessError` | If the command finished with a non-zero return code. |

#### method `resume`

```python
def resume()
```

Resume the process.

#### method `suspend`

```python
def suspend()
```

Suspend the process.

#### method `wait`

```python
def wait(timeout: float | None = None) -> int
```

Wait for the process to terminate.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `timeout` | `float | None` | Maximum time in seconds to wait. Ignored if a timeout was given when the handle was created. | `None` |

Returns

| Type | Description |
| --- | --- |
| `int` | The return code of the process. |

Raises

| Type | Description |
| --- | --- |
| `subprocess.TimeoutExpired` | If the process did not finish within the timeout. Unlike a truth test, this leaves the process running. |

### class `SubprocessResult`

```python
class SubprocessResult(subprocess.CompletedProcess)
```

Extension of `subprocess.CompletedProcess` that also carries peak memory usage.

Attributes

| Name | Description |
| --- | --- |
| `peak_memory` | Peak memory usage of the process and its children, in bytes. |
| `total_time` | Wall-clock run time of the process, in seconds. |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Classes-SubprocessResult-__init__) | Initialize the result. |

#### method `__init__`

```python
def __init__(
    args: list[str],
    returncode: int,
    stdout: bytes | None = None,
    stderr: bytes | None = None,
    *,
    peak_memory: int,
    total_time: float,
)
```

Initialize the result.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `args` | `list[str]` | Command that was run. | required |
| `returncode` | `int` | Exit status of the process. | required |
| `stdout` | `bytes | None` | Captured standard output. | `None` |
| `stderr` | `bytes | None` | Captured standard error. | `None` |
| `peak_memory` | `int` | Peak memory usage of the process and its children, in bytes. | required |
| `total_time` | `float` | Wall-clock run time of the process, in seconds. | required |

## Functions

| Name | Description |
| --- | --- |
| [`strip_ansi`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Functions-strip_ansi) | Remove ANSI escape sequences from a string. |
| [`subprocess_run`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Functions-subprocess_run) | Run a command and block until it finishes. |

### function `strip_ansi`

```python
def strip_ansi(s: str) -> str
```

Remove ANSI escape sequences from a string.

> **Example**
> ```pycon
>>> strip_ansi("\x1b[31mred\x1b[0m")
'red'
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `s` | `str` | String to strip. | required |

Returns

| Type | Description |
| --- | --- |
| `str` | The string without ANSI escape sequences. |

### function `subprocess_run`

```python
def subprocess_run(
    cmd: str | Sequence[PathType],
    *,
    silent: bool = False,
    timeout: float | None = None,
) -> SubprocessResult
```

Run a command and block until it finishes.

Backwards-compatible wrapper around [SubprocessHandle](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/subprocess.md#subprocess-Classes-SubprocessHandle).

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `cmd` | `str | Sequence[PathType]` | Command to execute. If a string is given, it will be split on whitespace. If a list is given, each element will be converted to a string. | required |
| `silent` | `bool` | If `True`, suppress all output from the command. | `False` |
| `timeout` | `float | None` | If given, the maximum time in seconds to allow the process to run. If the timeout is exceeded, the process is terminated and `subprocess.TimeoutExpired` is raised. | `None` |

Returns

| Type | Description |
| --- | --- |
| `SubprocessResult` | Result of the command. |
