Python 如何摆脱等待服务器响应的阻塞生成器?
Python how do I break out of blocking generator that is waiting for server response?
所以我有这段代码可以使用 Google 云语音 API 将用户语音转录成文本。它是根据 Google 中的示例编写的:https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/cloud-client/transcribe_streaming_mic.py
我想添加一个错误处理程序,它会在出现 Internet 连接问题时停止转录过程。我创建了一个连接监视器线程,每隔几秒检查一次互联网连接,并设置一个标志 isConnectionError = True
.
我设法停止了录音生成器进程,但我无法停止阻止并等待服务器发送响应消息的另一个生成器进程:
def listen_print_loop(responses):
"""Iterates through server responses and prints them.
The responses passed is a generator that will block until a response
is provided by the server.
Each response may contain multiple results, and each result may contain
multiple alternatives; for details, see <url removed>. Here we
print only the transcription for the top alternative of the top result.
In this case, responses are provided for interim results as well. If the
response is an interim one, print a line feed at the end of it, to allow
the next result to overwrite it, until the response is a final one. For the
final one, print a newline to preserve the finalized transcription.
"""
num_chars_printed = 0
for response in responses:
if not response.results:
continue
# The `results` list is consecutive. For streaming, we only care about
# the first result being considered, since once it's `is_final`, it
# moves on to considering the next utterance.
result = response.results[0]
if not result.alternatives:
continue
# Display the transcription of the top alternative.
transcript = result.alternatives[0].transcript
# Display interim results, but with a carriage return at the end of the
# line, so subsequent lines will overwrite them.
#
# If the previous result was longer than this one, we need to print
# some extra spaces to overwrite the previous result
overwrite_chars = ' ' * (num_chars_printed - len(transcript))
if not result.is_final:
sys.stdout.write(transcript + overwrite_chars + '\r')
sys.stdout.flush()
num_chars_printed = len(transcript)
else:
print(transcript + overwrite_chars)
# Exit recognition if any of the transcribed phrases could be
# one of our keywords.
if re.search(r'\b(exit|quit)\b', transcript, re.I):
print('Exiting..')
break
num_chars_printed = 0
我们通过监听器和发射器上的 websockets 实现了相同的连接,其中语音到文本从网页到 Java 服务
根据 Kenny 的建议,我只是 运行 生成器在一个单独的线程中。完美运行
所以我有这段代码可以使用 Google 云语音 API 将用户语音转录成文本。它是根据 Google 中的示例编写的:https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/cloud-client/transcribe_streaming_mic.py
我想添加一个错误处理程序,它会在出现 Internet 连接问题时停止转录过程。我创建了一个连接监视器线程,每隔几秒检查一次互联网连接,并设置一个标志 isConnectionError = True
.
我设法停止了录音生成器进程,但我无法停止阻止并等待服务器发送响应消息的另一个生成器进程:
def listen_print_loop(responses):
"""Iterates through server responses and prints them.
The responses passed is a generator that will block until a response
is provided by the server.
Each response may contain multiple results, and each result may contain
multiple alternatives; for details, see <url removed>. Here we
print only the transcription for the top alternative of the top result.
In this case, responses are provided for interim results as well. If the
response is an interim one, print a line feed at the end of it, to allow
the next result to overwrite it, until the response is a final one. For the
final one, print a newline to preserve the finalized transcription.
"""
num_chars_printed = 0
for response in responses:
if not response.results:
continue
# The `results` list is consecutive. For streaming, we only care about
# the first result being considered, since once it's `is_final`, it
# moves on to considering the next utterance.
result = response.results[0]
if not result.alternatives:
continue
# Display the transcription of the top alternative.
transcript = result.alternatives[0].transcript
# Display interim results, but with a carriage return at the end of the
# line, so subsequent lines will overwrite them.
#
# If the previous result was longer than this one, we need to print
# some extra spaces to overwrite the previous result
overwrite_chars = ' ' * (num_chars_printed - len(transcript))
if not result.is_final:
sys.stdout.write(transcript + overwrite_chars + '\r')
sys.stdout.flush()
num_chars_printed = len(transcript)
else:
print(transcript + overwrite_chars)
# Exit recognition if any of the transcribed phrases could be
# one of our keywords.
if re.search(r'\b(exit|quit)\b', transcript, re.I):
print('Exiting..')
break
num_chars_printed = 0
我们通过监听器和发射器上的 websockets 实现了相同的连接,其中语音到文本从网页到 Java 服务
根据 Kenny 的建议,我只是 运行 生成器在一个单独的线程中。完美运行