Source code for nemo_deploy.ray_utils

# Copyright (c) 2025, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import socket


[docs] def is_port_in_use(port: int, host: str = "0.0.0.0") -> bool: """Check if a given port is already in use. Args: port (int): The port number to check. Returns: bool: True if the port is in use, False otherwise. """ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: try: s.bind((host, port)) return False except socket.error: return True
[docs] def find_available_port(start_port: int, host: str = "0.0.0.0") -> int: """Find the next available port starting from a given port number. Args: start_port (int): The port number to start checking from. Returns: int: The first available port number found. """ port = start_port while is_port_in_use(port, host): port += 1 return port