您需要设置两个并行进程,但与任务队列相互关联。
这是因为您依赖于记录器进程的提取。
这是实现这一目标的一种方法的尝试(显然它没有经过完善,可以进一步改进):
def recorder_process(recorder_queue, extractor_queue):
speech_config = speechsdk.SpeechConfig(subscription="", region="")
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)
while True:
request = recorder_queue.get()
result = speech_recognizer.recognize_once_async().get()
extractor_queue.put(result.text)
def extractor_process(extractor_queue, results_queue):
while True:
transcript = extractor_queue.get()
summa_keywords = summa_keyword_extractor.keywords(transcript, ratio=1.0)
results_queue.put({'transcript': transcript, 'keywords': summa_keywords})
if __name__ == "__main__":
# Connect to remote host over TCP
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST,PORT))
# Set up a Queue to pass data to the update process, and another one
# for the two children to communicate
recorder_queue = Queue()
extractor_queue = Queue()
results_queue = Queue()
# Create two child processes, pass a reference to the Queue to each
recorder = Process(target=recorder_process, args=(recorder_queue, extractor_queue))
extractor = Process(target=extractor_process, args=(extractor_queue, results_queue))
recorder.start()
extractor.start()
index = 0
while True:
recorder_queue.put(index)
index += 1
sleep(1)
recorder.join()
extractor.join()