需要同时运行 2个功能但是,但是他们只有运行一个接一个

Need to run 2 functions at the same time but, but they only run one after another

我有两个功能

def Print_Out(string):
    typing_speed = Engine.getProperty('rate') #wpm
    for c in string:
        print(c, end='')
        time.sleep(random.random()*10.0/typing_speed)
    print('')

将以 200 wpm 的速度缓慢打印文本 和另一个函数 "Say",它将向用户读取该文本(使用 pyttsx3)。

我试过使用多线程

threading.thread(target = Print_Out(Response)).start()
threading.thread(target = Say(Response)).start()

(我也试过最后没有“.start()”但仍然 运行 的功能) 我尝试了多处理,但我不确定我做对了,我不能为此提供代码,因为我只是拿了我发现的东西并尝试在这里使用它

我需要它们 运行 并行,当文本打印出来时,声音在说,但最终发生的是它慢慢地打印出文本然后读取它(或其他方式周围,​​取决于它们如何放置在代码中)。 没有错误

当您尝试制作线程版本时,您实际上传递了调用预期目标(使用 Response 作为参数)而不是函数的结果。

所以函数首先被调用,等待 return,然后响应(无论是什么)作为 target 参数传递给 Thread

试试这个:

t1 = threading.Thread(target=Print_Out, args=(Response,))
t2 = threading.Thread(target=Say, args=(Response,))
t1.start()
t2.start()
# And wait for the threads to finish
t1.join()
t2.join()

(即传递函数本身)