11# -*- coding: utf-8 -*-
22"""DashScope Realtime TTS model implementation."""
3+ import asyncio
34import base64
45import threading
5- import time
66from typing import Any , AsyncGenerator , Literal , TYPE_CHECKING
77
88from pydantic import BaseModel , Field
@@ -71,9 +71,7 @@ def on_event(self, response: dict[str, Any]) -> None:
7171 self .finish_event .set ()
7272
7373 except Exception :
74- import traceback
75-
76- traceback .print_exc ()
74+ logger .exception ("Error in TTS WebSocket callback" )
7775 self .finish_event .set ()
7876
7977 def on_close (self , close_status_code : int , close_msg : str ) -> None :
@@ -148,7 +146,7 @@ async def get_audio_chunks(self) -> AsyncGenerator[TTSResponse, None]:
148146 if self .chunk_event .is_set ():
149147 self .chunk_event .clear ()
150148 else :
151- self .chunk_event .wait ( )
149+ await asyncio . to_thread ( self .chunk_event .wait )
152150
153151 if self .finish_event .is_set ():
154152 continue
@@ -410,7 +408,9 @@ async def synthesize(
410408 self ._tts_client .commit ()
411409 self ._tts_client .finish ()
412410
413- self ._callback .finish_event .wait ()
411+ await asyncio .to_thread (
412+ self ._callback .finish_event .wait ,
413+ )
414414
415415 if full_text and not self ._callback .has_audio_data ():
416416 if attempt < self .max_retries - 1 :
@@ -421,7 +421,7 @@ async def synthesize(
421421 self .max_retries ,
422422 delay ,
423423 )
424- time .sleep (delay )
424+ await asyncio .sleep (delay )
425425 await self ._reconnect ()
426426 unsent = full_text
427427 delay *= 2
@@ -441,7 +441,7 @@ async def synthesize(
441441 self .max_retries ,
442442 delay ,
443443 )
444- time .sleep (delay )
444+ await asyncio .sleep (delay )
445445 await self ._reconnect ()
446446 unsent = full_text
447447 delay *= 2
0 commit comments