Files
zsglpt/services/runtime.py

37 lines
890 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
运行时依赖注入:用于 services/routes 访问 socketio/logger 等全局实例。
说明:
- 仅在 app.py 启动装配时调用 init_runtime()
- 业务模块中避免直接 import app.py统一通过本模块获取依赖
"""
from __future__ import annotations
from typing import Any, Optional
_socketio: Optional[Any] = None
_logger: Optional[Any] = None
def init_runtime(*, socketio: Any, logger: Any) -> None:
global _socketio, _logger
_socketio = socketio
_logger = logger
def get_socketio() -> Any:
if _socketio is None:
raise RuntimeError("socketio 未初始化(请先在 app.py 调用 init_runtime")
return _socketio
def get_logger() -> Any:
if _logger is None:
raise RuntimeError("logger 未初始化(请先在 app.py 调用 init_runtime")
return _logger