Skip to content

Commit c27a3dc

Browse files
authored
Update deploy.py (#3310)
* Update deploy.py 更新部署工具 * Update deploy.py
1 parent c56c998 commit c27a3dc

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

test/ce/deploy/deploy.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717

1818
def get_base_port():
19-
nv_visible_devices = os.environ.get("NVIDIA_VISIBLE_DEVICES", "")
19+
"""获取base port"""
20+
nv_visible_devices = os.environ.get("CUDA_VISIBLE_DEVICES", "")
2021
if not nv_visible_devices or nv_visible_devices.lower() == "all":
2122
return 8000
2223
# 提取第一个数字
@@ -26,14 +27,37 @@ def get_base_port():
2627
return 8000
2728

2829

30+
def is_port_in_use(port):
31+
"""检查端口是否被占用"""
32+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
33+
return s.connect_ex(("localhost", port)) == 0
34+
35+
36+
def get_available_port(env_key: str, default_start: int):
37+
"""从环境变量读取端口,如果未设置或已被占用,则从default_start开始寻找空闲端口"""
38+
port_str = os.environ.get(env_key)
39+
if port_str and port_str.isdigit():
40+
port = int(port_str)
41+
if not is_port_in_use(port):
42+
return port
43+
else:
44+
print(f"Warning: Port {port} from {env_key} is in use, searching for a free port...")
45+
46+
# 从 default_start 开始查找空闲端口
47+
port = default_start
48+
while is_port_in_use(port):
49+
port += 1
50+
return port
51+
52+
2953
# 默认参数值
3054
PID_FILE = "pid_port"
3155
LOG_FILE = "server.log"
3256
base_port = get_base_port()
33-
FLASK_PORT = int(os.environ.get("FLASK_PORT", base_port + 1))
34-
FD_API_PORT = int(os.environ.get("FD_API_PORT", base_port + 2))
35-
FD_ENGINE_QUEUE_PORT = int(os.environ.get("FD_ENGINE_QUEUE_PORT", base_port + 3))
36-
FD_METRICS_PORT = int(os.environ.get("FD_METRICS_PORT", base_port + 4))
57+
FLASK_PORT = get_available_port("FLASK_PORT", base_port + 1)
58+
FD_API_PORT = get_available_port("FD_API_PORT", FLASK_PORT + 1)
59+
FD_ENGINE_QUEUE_PORT = get_available_port("FD_ENGINE_QUEUE_PORT", FD_API_PORT + 1)
60+
FD_METRICS_PORT = get_available_port("FD_METRICS_PORT", FD_ENGINE_QUEUE_PORT + 1)
3761
DEFAULT_PARAMS = {
3862
"--port": FD_API_PORT,
3963
"--engine-worker-queue-port": FD_ENGINE_QUEUE_PORT,
@@ -73,12 +97,6 @@ def merge_configs(base_config, override_config):
7397
return merged
7498

7599

76-
def is_port_in_use(port):
77-
"""检查端口是否被占用"""
78-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
79-
return s.connect_ex(("localhost", port)) == 0
80-
81-
82100
def get_server_pid():
83101
"""获取服务进程ID PORT"""
84102
if os.path.exists(PID_FILE):
@@ -105,7 +123,8 @@ def is_server_running():
105123
try:
106124
response = requests.get(health_check_endpoint, timeout=2)
107125
return response.status_code == 200, result
108-
except requests.exceptions.RequestException:
126+
except requests.exceptions.RequestException as e:
127+
print(f"Failed to check server health: {e}")
109128
return False, result
110129

111130

@@ -158,14 +177,14 @@ def stop_server(signum=None, frame=None):
158177
except Exception as e:
159178
print(f"Failed to stop server: {e}")
160179

161-
for port in [FD_API_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT]:
162-
try:
163-
output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip()
164-
for pid in output.splitlines():
165-
os.kill(int(pid), signal.SIGKILL)
166-
print(f"Killed process on port {port}, pid={pid}")
167-
except Exception as e:
168-
print(f"Failed to killed process on port: {e}")
180+
for port in [FD_API_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT]:
181+
try:
182+
output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip()
183+
for pid in output.splitlines():
184+
os.kill(int(pid), signal.SIGKILL)
185+
print(f"Killed process on port {port}, pid={pid}")
186+
except Exception as e:
187+
print(f"Failed to killed process on port: {e}")
169188
# 若log目录存在,则重命名为log_timestamp
170189
if os.path.isdir("./log"):
171190
os.rename("./log", "./log_{}".format(time.strftime("%Y%m%d%H%M%S")))
@@ -196,6 +215,7 @@ def start_service():
196215
base_config = DEFAULT_PARAMS
197216

198217
override_config = request.get_json() or {}
218+
print("override_config", override_config)
199219

200220
final_config = merge_configs(base_config, override_config)
201221

0 commit comments

Comments
 (0)