Skip to content

Commit 02078ad

Browse files
committed
port random generate
1 parent cac2edf commit 02078ad

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

lightllm/utils/net_utils.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import subprocess
33
import ipaddress
44
import random
5-
import portpicker
65
from lightllm.utils.log_utils import init_logger
76

87
logger = init_logger(__name__)
@@ -13,25 +12,37 @@ def alloc_can_use_network_port(num=3, used_nccl_ports=None, from_port_num=10000)
1312
used_nccl_ports = []
1413

1514
port_list = []
15+
locked_sockets = []
16+
used_set = set(used_nccl_ports)
17+
max_port = 65535
1618
max_attempts = num * 50 # Allow more attempts to find ports in range
1719

1820
for _ in range(max_attempts):
1921
if len(port_list) >= num:
2022
break
2123

22-
try:
23-
port = portpicker.pick_unused_port()
24-
25-
if port >= from_port_num and port not in used_nccl_ports:
26-
port_list.append(port)
27-
logger.debug(f"Allocated port: {port}")
28-
else:
29-
logger.debug(f"Port {port} is out of range or in used_nccl_ports, skipping")
24+
# 在 [from_port_num, 65535] 范围内随机选端口,避免多进程同时启动时分配到相同端口
25+
port = random.randint(from_port_num, max_port)
26+
if port in used_set:
27+
continue
3028

31-
except Exception as e:
32-
logger.warning(f"Failed to allocate port: {e}")
29+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
30+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
31+
try:
32+
sock.bind(("", port))
33+
port_list.append(port)
34+
used_set.add(port)
35+
locked_sockets.append(sock)
36+
logger.debug(f"Allocated and locked port: {port}")
37+
38+
except OSError as e:
39+
sock.close()
40+
logger.warning(f"Failed to bind port: {e}")
3341
continue
3442

43+
for sock in locked_sockets:
44+
sock.close()
45+
3546
if len(port_list) < num:
3647
logger.error(f"Failed to allocate {num} ports, only got {len(port_list)}")
3748
return None

0 commit comments

Comments
 (0)