Open FDUS105301 opened 6 months ago
@yulin-li Can you please check.
Here is my solution to avoid blocking flow when using .get method from azure python
def your_method_is_blocked_util_get_result(your params,...): .... result = speech_synthesizer.speak_text_async(text).get()
....
async def wrap_async_method: result_method = asyncio.to_thread( your_method_is_blocked_util_get_result , your params) await result_method
your main flow
await wrap_async_method(param....)
Here is my solution to avoid blocking flow when using .get method from azure python
def your_method_is_blocked_util_get_result(your params,...): .... result = speech_synthesizer.speak_text_async(text).get()
....
async def wrap_async_method: result_method = asyncio.to_thread( your_method_is_blocked_util_get_result , your params) await result_method
your main flow
await wrap_async_method(param....)
Let me try this solution. Am i correct in assuming that this is a workaround more than a full on solution?
It's true that the speak_text_async()
API is not an asyncio API in Python. Actually the Speech SDK doesn't support asyncio now.
Here is my solution to avoid blocking flow when using .get method from azure python def your_method_is_blocked_util_get_result(your params,...): .... result = speech_synthesizer.speak_text_async(text).get() .... async def wrap_async_method: result_method = asyncio.to_thread( your_method_is_blocked_util_get_result , your params) await result_method your main flow await wrap_async_method(param....)
Let me try this solution. Am i correct in assuming that this is a workaround more than a full on solution?
yes. azure async is not an async API. So you should put this blocking to another thread, while your main runs on main thread.
I have this line of code
result = speech_synthesizer.speak_text_async(text).get()
which is supposed to be async. I have created a callback handler to intercept the streamed data in chunks like so
`class MyPushAudioOutputStream(speechsdk.audio.PushAudioOutputStreamCallback): def init(self, frame_rate, sample_width, channels): super().init() self.frame_rate = frame_rate self.sample_width = sample_width self.channels = channels self.audio_data = bytearray() self.chunk_count = 0
I am able to get the data right as its coming in. emitting the daat through sockets is not working untill synthesis is complete (speak_text_async). It is blocking the emit behavior of sockets. However the function specifically has a comment description saying "Performs synthesis on plain text in a non-blocking (asynchronous) mode." which is clearly not the case
Here is my main code
`@socketio.on('synthesize_text') def handle_synthesize_text(data): start_time = time.time() text = data['text'] print('Speaking!!!')
I am not able to stream to my socket in realtime because of this error.
Version of the Cognitive Services Speech SDK: 1.37.0
Which version of the SDK are you using: Latest.
Platform, Operating System, and Programming Language
Additional information: I'm using sockets in async mode and monkey patching with gevent. The code is hosted on an azure web app (Linux) basic b1 tier.