Python如何打破等待服务器响应的阻塞生成器?

时间:2017-09-12 14:59:22

标签: python generator google-cloud-speech

所以我有这个代码可以使用Google Cloud Speech API将用户语音转录为文本。它是基于Google的这个示例编写的:https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/cloud-client/transcribe_streaming_mic.py

我想添加一个错误处理程序,可以在出现互联网连接问题时停止转录过程。我创建了一个连接监视器线程,每隔几秒检查一次Internet连接,并设置一个标记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

我设法停止录音生成器进程,但我无法阻止另一个阻止服务器发送响应消息的生成器进程:

#coding = utf-8

import json
import tweepy
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.streaming import StreamListener

consumer_key = "my_consumer_key"
consumer_secret = "my_consumer_secret"
access_token = "my_acess_token"
access_token_secret = "my_acess_token_secret"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

def saida_json(tweet):
    with open('tweet.json', 'a', encoding='utf-8') as f:
        json.dump(tweet, f)

def saida_txt(tweet):
    with open('tweet.txt', 'a', encoding='utf-8') as f:
        for linha in tweet:
            f.write(tweet + '\n')

name = "usersl"
tweetCount = 20
public_tweets = api.home_timeline()
user_tweets = api.user_timeline(id=name, count=tweetCount)

for tweet in user_tweets:
    print(tweet.user.screen_name, tweet.text)
    saida_txt(tweet.text)
    saida_json(tweet)

2 个答案:

答案 0 :(得分:0)

我们通过侦听器和发射器上的websockets实现了相同的连接,从而将语音到文本从网页转移到Java服务

答案 1 :(得分:0)

正如Kenny所说,我只是在一个单独的线程中运行生成器。完美无瑕地工作