import socket

# 启动 TensorBoard 子进程前添加端口检测逻辑
def find_available_port(start_port):
    port = start_port
    while True:
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            if s.connect_ex(('localhost', port)) != 0:  # 端口未被占用
                return port
            port += 1  # 如果端口被占用，尝试下一个端口

if __name__ == "__main__":
    start_port = 6006  # 起始端口号
    available_port = find_available_port(start_port)
    print(f"Available port: {available_port}")