Skip to content

Commit ec9c797

Browse files
Consolidate find_free_port implementations to ray._common.network_utils with type hints
Signed-off-by: Yicheng-Lu-llll <[email protected]>
1 parent eec5c69 commit ec9c797

File tree

13 files changed

+29
-35
lines changed

13 files changed

+29
-35
lines changed

python/ray/_common/network_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import socket
2+
from contextlib import closing
23
from functools import lru_cache
34
from typing import Optional, Tuple, Union
45

@@ -93,3 +94,19 @@ def is_localhost(host: str) -> bool:
9394
True if the host is a localhost address, False otherwise.
9495
"""
9596
return host in ("localhost", "127.0.0.1", "::1")
97+
98+
99+
def find_free_port(family: socket.AddressFamily = socket.AF_INET) -> int:
100+
"""Find a free port on the local machine.
101+
102+
Args:
103+
family: The socket address family (AF_INET for IPv4, AF_INET6 for IPv6).
104+
Defaults to AF_INET.
105+
106+
Returns:
107+
An available port number.
108+
"""
109+
with closing(socket.socket(family, socket.SOCK_STREAM)) as s:
110+
s.bind(("", 0))
111+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
112+
return s.getsockname()[1]

python/ray/_private/test_utils.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,14 +1826,6 @@ def job_hook(**kwargs):
18261826
sys.exit(0)
18271827

18281828

1829-
def find_free_port() -> int:
1830-
sock = socket.socket()
1831-
sock.bind(("", 0))
1832-
port = sock.getsockname()[1]
1833-
sock.close()
1834-
return port
1835-
1836-
18371829
def wandb_setup_api_key_hook():
18381830
"""
18391831
Example external hook to set up W&B API key in

python/ray/air/_internal/util.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
import logging
33
import os
44
import queue
5-
import socket
65
import threading
7-
from contextlib import closing
86
from typing import Optional
97

108
import numpy as np
@@ -14,13 +12,6 @@
1412
logger = logging.getLogger(__name__)
1513

1614

17-
def find_free_port(family=socket.AF_INET):
18-
with closing(socket.socket(family, socket.SOCK_STREAM)) as s:
19-
s.bind(("", 0))
20-
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
21-
return s.getsockname()[1]
22-
23-
2415
def is_nan(value):
2516
return np.isnan(value)
2617

python/ray/dashboard/modules/aggregator/tests/test_aggregator_agent.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
from google.protobuf.timestamp_pb2 import Timestamp
99

1010
import ray.dashboard.consts as dashboard_consts
11+
from ray._common.network_utils import find_free_port
1112
from ray._private import ray_constants
12-
from ray._private.test_utils import (
13-
find_free_port,
14-
wait_for_condition,
15-
)
13+
from ray._private.test_utils import wait_for_condition
1614
from ray._private.utils import init_grpc_channel
1715
from ray._raylet import GcsClient
1816
from ray.core.generated.common_pb2 import (

python/ray/dashboard/modules/reporter/tests/test_healthz.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import requests
55

66
import ray._private.ray_constants as ray_constants
7+
from ray._common.network_utils import find_free_port
78
from ray._common.test_utils import wait_for_condition
8-
from ray._private.test_utils import find_free_port
99
from ray.tests.conftest import * # noqa: F401 F403
1010

1111

python/ray/tests/conftest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222

2323
import ray
2424
import ray._private.ray_constants as ray_constants
25-
from ray._common.network_utils import build_address
25+
from ray._common.network_utils import build_address, find_free_port
2626
from ray._common.test_utils import wait_for_condition
2727
from ray._private.conftest_utils import set_override_dashboard_url # noqa: F401
2828
from ray._private.runtime_env import virtualenv_utils
2929
from ray._private.test_utils import (
3030
RayletKiller,
3131
external_redis_test_enabled,
32-
find_free_port,
3332
get_and_run_resource_killer,
3433
get_redis_cli,
3534
init_error_pubsub,

python/ray/tests/test_gcs_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
import ray
1212
import ray._private.gcs_utils as gcs_utils
1313
import ray._private.ray_constants as ray_constants
14-
from ray._common.network_utils import parse_address
14+
from ray._common.network_utils import find_free_port, parse_address
1515
from ray._common.test_utils import async_wait_for_condition
1616
from ray._private.test_utils import (
1717
external_redis_test_enabled,
18-
find_free_port,
1918
generate_system_config_map,
2019
)
2120
from ray._raylet import GcsClient, NodeID

python/ray/tests/test_metrics_agent.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from google.protobuf.timestamp_pb2 import Timestamp
1717

1818
import ray
19-
from ray._common.network_utils import build_address
19+
from ray._common.network_utils import build_address, find_free_port
2020
from ray._common.test_utils import SignalActor, wait_for_condition
2121
from ray._private.metrics_agent import (
2222
Gauge as MetricsAgentGauge,
@@ -27,7 +27,6 @@
2727
PrometheusTimeseries,
2828
fetch_prometheus_metric_timeseries,
2929
fetch_prometheus_timeseries,
30-
find_free_port,
3130
get_log_batch,
3231
raw_metric_timeseries,
3332
)

python/ray/tests/test_ray_event_export_task_events.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
import ray
1010
import ray.dashboard.consts as dashboard_consts
11+
from ray._common.network_utils import find_free_port
1112
from ray._common.test_utils import wait_for_condition
1213
from ray._private import ray_constants
13-
from ray._private.test_utils import find_free_port, run_string_as_driver_nonblocking
14+
from ray._private.test_utils import run_string_as_driver_nonblocking
1415
from ray._raylet import GcsClient
1516

1617
logger = logging.getLogger(__name__)

python/ray/tests/test_runtime_env_working_dir_3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
import ray
1111
import ray.experimental.internal_kv as kv
12+
from ray._common.network_utils import find_free_port
1213
from ray._common.test_utils import wait_for_condition
1314
from ray._private.ray_constants import RAY_RUNTIME_ENV_URI_PIN_EXPIRATION_S_ENV_VAR
1415
from ray._private.test_utils import (
1516
chdir,
1617
check_local_files_gced,
17-
find_free_port,
1818
)
1919
from ray._private.utils import get_directory_size_bytes
2020

0 commit comments

Comments
 (0)