Skip to content

Commit 4d2a31a

Browse files
authored
chore: add port reservation to utils (#1980)
1 parent 1e3e4a0 commit 4d2a31a

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

examples/sglang/utils/sgl_utils.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
# limitations under the License.
1515

1616
import argparse
17+
import contextlib
18+
import socket
1719
from argparse import Namespace
1820

1921
from sglang.srt.server_args import ServerArgs
2022

21-
from dynamo.sdk.cli.utils import reserve_free_port
22-
2323

2424
def parse_sglang_args_inc(args: list[str]) -> ServerArgs:
2525
parser = argparse.ArgumentParser()
@@ -33,6 +33,20 @@ def parse_sglang_args_inc(args: list[str]) -> ServerArgs:
3333
return ServerArgs.from_cli_args(parsed_args)
3434

3535

36+
@contextlib.contextmanager
37+
def reserve_free_port(host: str = "localhost"):
38+
"""
39+
Find and reserve a free port until context exits.
40+
"""
41+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
42+
try:
43+
sock.bind((host, 0))
44+
_, port = sock.getsockname()
45+
yield port
46+
finally:
47+
sock.close()
48+
49+
3650
def _reserve_disaggregation_bootstrap_port():
3751
"""
3852
Each worker requires a unique port for disaggregation_bootstrap_port.

0 commit comments

Comments
 (0)