> For the complete documentation index, see [llms.txt](https://docs.sov.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sov.ai/api-reference/utils/port-manager.md).

# Port Manager

**Module:** `sovai.utils.port_manager`

## Classes

### `PortManager`

```python
class PortManager
```

Manages the allocation of unique network ports for applications. Includes robust checks for port availability and can attempt to free ports.

**Attributes**

* `app_ports` (`Dict[str, int]`)
* `min_port`
* `max_port`

**Methods**

### `__init__()`

```python
def __init__(self, min_port: int = 8050, max_port: int = 8099)
```

Initializes the PortManager.

**Parameters**

| Parameter  | Type  | Description                                                                                                      |
| ---------- | ----- | ---------------------------------------------------------------------------------------------------------------- |
| `min_port` | `int` | The minimum port number in the range to assign. max\_port (int): The maximum port number in the range to assign. |

***

### `is_port_in_use()`

```python
def is_port_in_use(self, port: int) -> bool
```

Checks if a port is currently in use on the system. Tries to bind to the port; if it fails, the port is in use.

**Parameters**

| Parameter | Type  | Description               |
| --------- | ----- | ------------------------- |
| `port`    | `int` | The port number to check. |

**Returns**

bool: True if the port is in use, False otherwise.

***

### `kill_process_on_port()`

```python
def kill_process_on_port(self, port: int) -> bool
```

Finds and kills processes listening on the given port.

**Parameters**

| Parameter | Type  | Description                 |
| --------- | ----- | --------------------------- |
| `port`    | `int` | The port number to free up. |

**Returns**

bool: True if any process was attempted to be killed, False otherwise.

***

### `get_unique_port()`

```python
def get_unique_port(self, app_name: str) -> int
```

Gets a unique port for the given application name. If a port was already assigned to this app\_name in this session, tries to reuse it if free. Otherwise, finds a new free port in the defined range.

**Parameters**

| Parameter  | Type  | Description                                   |
| ---------- | ----- | --------------------------------------------- |
| `app_name` | `str` | The name of the application requiring a port. |

**Returns**

int: A unique port number.

***

### `release_port()`

```python
def release_port(self, app_name: str)
```

Releases a port associated with an app\_name, making it available for reuse by this manager. Note: This does not kill any process currently using the port system-wide.

**Parameters**

| Parameter  | Type  | Description                                               |
| ---------- | ----- | --------------------------------------------------------- |
| `app_name` | `str` | The name of the application whose port is to be released. |

***

***

## Functions

### `get_port_manager_instance()`

```python
def get_port_manager_instance(min_port: int = 8050, max_port: int = 8099) -> PortManager
```

Returns a cached singleton instance of PortManager. Allows customization of port range on first call.

**Parameters**

| Parameter  | Type  | Description                                                                                        |
| ---------- | ----- | -------------------------------------------------------------------------------------------------- |
| `min_port` | `int` | The minimum port number for the manager. max\_port (int): The maximum port number for the manager. |

**Returns**

PortManager: The singleton PortManager instance.

***

### `get_unique_port()`

```python
def get_unique_port(app_name: str, min_port: int = 8050, max_port: int = 8099) -> int
```

Gets a unique port for the given application name using the singleton PortManager. Port range can be specified, affecting the singleton on its first creation.

**Parameters**

| Parameter  | Type  | Description     |
| ---------- | ----- | --------------- |
| `app_name` | `str` | —               |
| `min_port` | `int` | Default: `8050` |
| `max_port` | `int` | Default: `8099` |

**Returns:** `int`

***

### `kill_process_on_port()`

```python
def kill_process_on_port(port: int, min_port: int = 8050, max_port: int = 8099) -> bool
```

Finds and kills processes listening on the given port using the singleton PortManager.

**Parameters**

| Parameter  | Type  | Description     |
| ---------- | ----- | --------------- |
| `port`     | `int` | —               |
| `min_port` | `int` | Default: `8050` |
| `max_port` | `int` | Default: `8099` |

**Returns:** `bool`

***

### `release_port()`

```python
def release_port(app_name: str, min_port: int = 8050, max_port: int = 8099)
```

Releases a port associated with an app\_name using the singleton PortManager.

**Parameters**

| Parameter  | Type  | Description     |
| ---------- | ----- | --------------- |
| `app_name` | `str` | —               |
| `min_port` | `int` | Default: `8050` |
| `max_port` | `int` | Default: `8099` |

***
