mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-26 12:14:02 +00:00
Fix port exception type (#2925)
This commit is contained in:
@@ -50,16 +50,20 @@ def ensure_path_exists(filename: str):
|
|||||||
Path(dirpath).mkdir(parents=True, exist_ok=True)
|
Path(dirpath).mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
def free_port():
|
def free_port() -> int:
|
||||||
|
"""Get a free port on localhost.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
int: A free port on localhost.
|
||||||
|
"""
|
||||||
while True:
|
while True:
|
||||||
|
port = random.randint(20000, 65000)
|
||||||
try:
|
try:
|
||||||
sock = socket.socket()
|
with socket.socket() as sock:
|
||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
port = random.randint(20000, 65000)
|
sock.bind(("localhost", port))
|
||||||
sock.bind(('localhost', port))
|
return port
|
||||||
sock.close()
|
except OSError:
|
||||||
return port
|
|
||||||
except Exception:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user