Python how do I break out of blocking generator that is waiting for server response? -


so have code take user speech , transcribe text using google cloud speech api. written based on example google: https://github.com/googlecloudplatform/python-docs-samples/blob/master/speech/cloud-client/transcribe_streaming_mic.py

i wanted add error handler stop transcription process whenever there's internet connection problem. create connection monitor thread check internet connection every few seconds , set flag isconnectionerror = true.

i manage stop audio recording generator process can't stop generator process block , waits server send response message:

def listen_print_loop(responses):  """iterates through server responses , prints them. responses passed generator block until response provided server. each response may contain multiple results, , each result may contain multiple alternatives; details, see <url removed>.  here print transcription top alternative of top result. in case, responses provided interim results well. if response interim one, print line feed @ end of it, allow next result overwrite it, until response final one. final one, print newline preserve finalized transcription. """ num_chars_printed = 0 response in responses:     if not response.results:         continue      # `results` list consecutive. streaming, care     # first result being considered, since once it's `is_final`,     # moves on considering next utterance.     result = response.results[0]     if not result.alternatives:         continue      # display transcription of top alternative.     transcript = result.alternatives[0].transcript      # display interim results, carriage return @ end of     # line, subsequent lines overwrite them.     #     # if previous result longer one, need print     # spaces overwrite 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 of transcribed phrases         # 1 of our keywords.         if re.search(r'\b(exit|quit)\b', transcript, re.i):             print('exiting..')             break          num_chars_printed = 0 


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -