refactor : FastAPI 懒加载单例,消除模块级副作用
- import astrai.inference 不再在模块加载时创建 FastAPI 实例 - 路由移至 APIRouter;get_app() 首次调用时懒构造单例 - _create_engine 和 run_server 的 param_path 改为必填 - 更新测试改用 get_app() 替代模块级 app
This commit is contained in:
@@ -5,21 +5,22 @@ from unittest.mock import MagicMock
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from astrai.inference import app
|
||||
from astrai.inference import get_app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
"""Provide a test client for the FastAPI app."""
|
||||
app.state.server_config = {
|
||||
_app = get_app()
|
||||
_app.state.server_config = {
|
||||
"device": "cpu",
|
||||
"dtype": "bfloat16",
|
||||
"param_path": None,
|
||||
"max_batch_size": 1,
|
||||
"_test": True,
|
||||
}
|
||||
app.state.engine = None
|
||||
return TestClient(app)
|
||||
_app.state.engine = None
|
||||
return TestClient(_app)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -49,5 +50,5 @@ def mock_engine():
|
||||
@pytest.fixture
|
||||
def loaded_model(client, mock_engine):
|
||||
"""Simulate that the engine is loaded."""
|
||||
app.state.engine = mock_engine
|
||||
get_app().state.engine = mock_engine
|
||||
return mock_engine
|
||||
|
||||
Reference in New Issue
Block a user