|
| 1 | +""" |
| 2 | +# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License" |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +# This file is modified from https://github.com/vllm-project/vllm/blob/main/vllm/entrypoints/cli/serve.py |
| 18 | + |
| 19 | +import argparse |
| 20 | +import atexit |
| 21 | +import os |
| 22 | +import signal |
| 23 | +import subprocess |
| 24 | +import sys |
| 25 | + |
| 26 | +from fastdeploy.entrypoints.cli.types import CLISubcommand |
| 27 | +from fastdeploy.entrypoints.openai.utils import make_arg_parser |
| 28 | +from fastdeploy.utils import FlexibleArgumentParser |
| 29 | + |
| 30 | + |
| 31 | +class ServeSubcommand(CLISubcommand): |
| 32 | + """The `serve` subcommand for the fastdeploy CLI.""" |
| 33 | + |
| 34 | + name = "serve" |
| 35 | + |
| 36 | + @staticmethod |
| 37 | + def cmd(args: argparse.Namespace) -> None: |
| 38 | + env = os.environ.copy() |
| 39 | + cmd = [ |
| 40 | + sys.executable, |
| 41 | + "-m", |
| 42 | + "fastdeploy.entrypoints.openai.api_server", |
| 43 | + *sys.argv[2:], |
| 44 | + ] |
| 45 | + |
| 46 | + # 启动子进程 |
| 47 | + proc = subprocess.Popen(cmd, env=env) |
| 48 | + print(f"Starting server (PID: {proc.pid})") |
| 49 | + |
| 50 | + # 定义清理函数 |
| 51 | + def cleanup(): |
| 52 | + """终止子进程并确保资源释放""" |
| 53 | + if proc.poll() is None: # 检查子进程是否仍在运行 |
| 54 | + print(f"\nTerminating child process (PID: {proc.pid})...") |
| 55 | + proc.terminate() # 发送终止信号 |
| 56 | + |
| 57 | + # 注册退出时的清理函数 |
| 58 | + atexit.register(cleanup) |
| 59 | + # 设置信号处理 |
| 60 | + |
| 61 | + def signal_handler(signum, frame): |
| 62 | + cleanup() |
| 63 | + sys.exit(0) |
| 64 | + |
| 65 | + # 捕获 SIGINT (Ctrl+C) 和 SIGTERM |
| 66 | + signal.signal(signal.SIGINT, signal_handler) |
| 67 | + signal.signal(signal.SIGTERM, signal_handler) |
| 68 | + # 主进程阻塞等待子进程 |
| 69 | + proc.wait() |
| 70 | + |
| 71 | + def subparser_init(self, subparsers: argparse._SubParsersAction) -> FlexibleArgumentParser: |
| 72 | + serve_parser = subparsers.add_parser( |
| 73 | + name=self.name, |
| 74 | + help="Start the FastDeploy OpenAI Compatible API server.", |
| 75 | + description="Start the FastDeploy OpenAI Compatible API server.", |
| 76 | + usage="fastdeploy serve [model_tag] [options]", |
| 77 | + ) |
| 78 | + serve_parser = make_arg_parser(serve_parser) |
| 79 | + serve_parser.add_argument("--config", help="Read CLI options from a config file. Must be a YAML file") |
| 80 | + return serve_parser |
| 81 | + |
| 82 | + |
| 83 | +def cmd_init() -> list[CLISubcommand]: |
| 84 | + return [ServeSubcommand()] |
0 commit comments