Port Manager
API reference for sovai.utils.port_manager
Module: sovai.utils.port_manager
Classes
PortManager
class PortManagerManages 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_portmax_port
Methods
__init__()
def __init__(self, min_port: int = 8050, max_port: int = 8099)Initializes the PortManager.
Parameters
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()
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
port
int
The port number to check.
Returns
bool: True if the port is in use, False otherwise.
kill_process_on_port()
Finds and kills processes listening on the given port.
Parameters
port
int
The port number to free up.
Returns
bool: True if any process was attempted to be killed, False otherwise.
get_unique_port()
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
app_name
str
The name of the application requiring a port.
Returns
int: A unique port number.
release_port()
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
app_name
str
The name of the application whose port is to be released.
Functions
get_port_manager_instance()
Returns a cached singleton instance of PortManager. Allows customization of port range on first call.
Parameters
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()
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
app_name
str
—
min_port
int
Default: 8050
max_port
int
Default: 8099
Returns: int
kill_process_on_port()
Finds and kills processes listening on the given port using the singleton PortManager.
Parameters
port
int
—
min_port
int
Default: 8050
max_port
int
Default: 8099
Returns: bool
release_port()
Releases a port associated with an app_name using the singleton PortManager.
Parameters
app_name
str
—
min_port
int
Default: 8050
max_port
int
Default: 8099
Last updated