mirror of https://github.com/hpcaitech/ColossalAI
Fix port exception type (#2925)
parent
61e687831d
commit
a848091141
|
@ -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:
|
||||||
try:
|
|
||||||
sock = socket.socket()
|
|
||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
||||||
port = random.randint(20000, 65000)
|
port = random.randint(20000, 65000)
|
||||||
sock.bind(('localhost', port))
|
try:
|
||||||
sock.close()
|
with socket.socket() as sock:
|
||||||
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
sock.bind(("localhost", port))
|
||||||
return port
|
return port
|
||||||
except Exception:
|
except OSError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue