perf(asr): 优化 SSL 上下文创建,避免阻塞事件循环
- 将 SSL 上下文初始化从 __init__ 延迟到 __aenter__ - 使用 asyncio.to_thread 在线程池中执行 SSL 上下文创建 - 避免在同步构造函数中执行潜在的阻塞操作
This commit is contained in:
@@ -35,9 +35,10 @@ class DoubaoASR:
|
|||||||
def __init__(self, config: Optional[ASRConfig] = None):
|
def __init__(self, config: Optional[ASRConfig] = None):
|
||||||
self.config = config
|
self.config = config
|
||||||
self._encoder = AudioEncoder(self.config)
|
self._encoder = AudioEncoder(self.config)
|
||||||
self._ssl_context = ssl.create_default_context()
|
self._ssl_context: Optional[ssl.SSLContext] = None
|
||||||
|
|
||||||
async def __aenter__(self) -> DoubaoASR:
|
async def __aenter__(self) -> DoubaoASR:
|
||||||
|
self._ssl_context = await asyncio.to_thread(ssl.create_default_context)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
|
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user