async context manager for FastAPI lifespan
(app: FastAPI)
| 166 | |
| 167 | @asynccontextmanager |
| 168 | async def lifespan(app: FastAPI): |
| 169 | """ |
| 170 | async context manager for FastAPI lifespan |
| 171 | """ |
| 172 | global engine_args |
| 173 | global _tracing_inited |
| 174 | import logging |
| 175 | |
| 176 | # Initialize tracing in worker lifecycle instead of module import time. |
| 177 | # This avoids creating grpc/cygrpc state before gunicorn forks workers. |
| 178 | if not _tracing_inited: |
| 179 | tracing.process_tracing_init() |
| 180 | _tracing_inited = True |
| 181 | |
| 182 | uvicorn_access = logging.getLogger("uvicorn.access") |
| 183 | uvicorn_access.handlers.clear() |
| 184 | |
| 185 | # 使用 gunicorn 的格式 |
| 186 | formatter = logging.Formatter("[%(asctime)s] [%(process)d] [INFO] %(message)s", datefmt="%Y-%m-%d %H:%M:%S") |
| 187 | |
| 188 | handler = logging.StreamHandler() |
| 189 | handler.setFormatter(formatter) |
| 190 | uvicorn_access.addHandler(handler) |
| 191 | uvicorn_access.propagate = False |
| 192 | |
| 193 | if args.tokenizer is None: |
| 194 | args.tokenizer = args.model |
| 195 | pid = args.port |
| 196 | api_server_logger.info(f"{pid}") |
| 197 | |
| 198 | if args.served_model_name is not None: |
| 199 | served_model_names = args.served_model_name |
| 200 | verification = True |
| 201 | else: |
| 202 | served_model_names = args.model |
| 203 | verification = False |
| 204 | model_paths = [ModelPath(name=served_model_names, model_path=args.model, verification=verification)] |
| 205 | |
| 206 | engine_args = EngineArgs.from_cli_args(args, skip_port_check=True) |
| 207 | fd_config = engine_args.create_engine_config() |
| 208 | if envs.FD_ENABLE_ASYNC_LLM: |
| 209 | os.environ["INFERENCE_MSG_QUEUE_ID"] = engine_args.engine_worker_queue_port[engine_args.local_data_parallel_id] |
| 210 | engine_client = EngineClient( |
| 211 | pid=pid, |
| 212 | port=int(os.environ.get("INFERENCE_MSG_QUEUE_ID", "0")), |
| 213 | fd_config=fd_config, |
| 214 | workers=args.workers, |
| 215 | max_logprobs=args.max_logprobs, |
| 216 | ) |
| 217 | await engine_client.connection_manager.initialize() |
| 218 | app.state.dynamic_load_weight = args.dynamic_load_weight |
| 219 | model_handler = OpenAIServingModels( |
| 220 | model_paths, |
| 221 | args.max_model_len, |
| 222 | args.ips, |
| 223 | ) |
| 224 | app.state.model_handler = model_handler |
| 225 | global llm_engine |
nothing calls this directly
no test coverage detected