首页 > 解决方案 > 我想在我的第一个 python 环境中调用第二个 python 环境中的函数。这可能吗?

问题描述

我正在开发一个虚拟助手。我正在使用 google_speech_to_text 转换器,我无法保持音频输入继续。我认为如果有任何方法可以使用两种环境,一种用于收听和转换文本,另一种用于其余处理。

我不想改变我的 STT 引擎。我只想知道是否可以同时在环境之间切换。如果是,如何?

这是我的 input.py 文件:无论我需要在哪里进行音频输入,我都会调用该函数start_listening()

import speech_recognition as sr
import output
import winsound

def start_listening():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        # output.speak("Listening")
        r.adjust_for_ambient_noise(source)
        audio = r.record(source, duration=5)

        try:
            return r.recognize_google(audio)
        except:
            output.speak("Unable to Translate, speak again")
            start_listening()

这是我的 processing.py 文件:

import input as listener
import output as speak
import clock
import query_processor as mind
import rideBooking

#First Greeting at the startup , according to the time select the greeting
speak.speak(clock.get_part_of_day())

def searching_for_word(word,sentence):
    if word in sentence:
        return True
    else:
        return False

def main_organ():
    input = listener.start_listening()
    inputType = mind.classify(input)
    if inputType == 'whatever':
        #run different functions on different commands
        main_organ()


#run the app with the below code
if __name__ == "__main__":
    main_organ()

在处理过程中,应用程序无法收听。只有在处理完全完成后才能开始监听。

标签: pythonpython-3.xenvironment-variablesvirtualenvenvironment

解决方案


您可以创建多个进程。

为此,请导入multiprocessing.Process.run模块并恢复返回值

您可以使用队列来处理来自子流程的数据。

您不需要多个环境。


推荐阅读