为什么带有 concurrent.futures.thread 的代码会跳过小部件操作?
Why does the code with the concurrent.futures.thread skip widget operations?
目标是首先对小部件执行操作(创建 Button_1 并删除 Button_2),然后启用语音识别。
我绝对应该使用 concurrent.futures.thread 模块。
在这次尝试中,编译器遍历每一行——这由标有 #for test
注释的行的输出证明。 问题:语音识别有效,但按钮操作(创建 Button_1 和删除 Button_2)仅在退出函数 voiceRecognition:
def someFunc(self, event):
print('Interface change started') #for test
#creates Button_1
#deletes config.Button_2 (from another func)
print('Interface change finished') #for test
with ThreadPoolExecutor(max_workers=5) as executor:
fut = executor.submit(self.voiceRecognition)
def voiceRecognition (self):
print('Voice recognition was started') #for test
r = sr.Recognizer()
with sr.Microphone(device_index=1) as sourse:
audio = r.listen(sourse)
query = r.recognize_google(audio)
print(query.lower())
print('Voice recognition was finished') #for test
输出:
Interface change started
Interface change finished
Voice recognition started
Voice recognition finished
这是专门针对按钮的(与上下文分开,它们可以正常工作):
config.Button_1 = wx.Button(self)
self.Bind(wx.EVT_BUTTON, self.eventFunc, Button_1)
config.Button_2.Destroy()
帮助我了解问题的原因及其实际解决方案。谢谢。
您遇到的问题是
1.you are only calling one thread for the voice recognition function which only runs one time
- my second guess would be that these functions are not i/o bound functions
线程的工作方式是另一个函数启动,而前一个函数正在等待接收特定数据或类似的东西。如果没有这样的要求那么线程不是很有用
第二个函数开始的点 运行 是第一个函数等待接收某些资源的点。如果这不是您的要求,您可能需要查看多处理或 async/await 函数(使用 asyncio 包)
import asyncio
async def someFunc(event):
print('Interface change started') #for test
#creates Button_1
await event() #when code runs with voiceRecognition past as the event parameter, that is what will run here
#deletes config.Button_2 (from another func)
print('Interface change finished') #for test
def voiceRecognition ():
print('Voice recognition was started') #for test
r = sr.Recognizer()
with sr.Microphone(device_index=1) as sourse:
audio = r.listen(sourse)
query = r.recognize_google(audio)
print(query.lower())
print('Voice recognition was finished') #for test
asyncio.run(someFunc(voiceRecognition)) #to run it asynchronously
我真的不知道你的 class 是什么样子,所以我把它写成一个典型的函数,这意味着你必须稍微调整它以适应你的代码。
Notice the change to how your voiceRecognition()
is called in someFunc
using your event parameter and also notice the position event is now called at in someFunc
目标是首先对小部件执行操作(创建 Button_1 并删除 Button_2),然后启用语音识别。
我绝对应该使用 concurrent.futures.thread 模块。
在这次尝试中,编译器遍历每一行——这由标有 #for test
注释的行的输出证明。 问题:语音识别有效,但按钮操作(创建 Button_1 和删除 Button_2)仅在退出函数 voiceRecognition:
def someFunc(self, event):
print('Interface change started') #for test
#creates Button_1
#deletes config.Button_2 (from another func)
print('Interface change finished') #for test
with ThreadPoolExecutor(max_workers=5) as executor:
fut = executor.submit(self.voiceRecognition)
def voiceRecognition (self):
print('Voice recognition was started') #for test
r = sr.Recognizer()
with sr.Microphone(device_index=1) as sourse:
audio = r.listen(sourse)
query = r.recognize_google(audio)
print(query.lower())
print('Voice recognition was finished') #for test
输出:
Interface change started
Interface change finished
Voice recognition started
Voice recognition finished
这是专门针对按钮的(与上下文分开,它们可以正常工作):
config.Button_1 = wx.Button(self)
self.Bind(wx.EVT_BUTTON, self.eventFunc, Button_1)
config.Button_2.Destroy()
帮助我了解问题的原因及其实际解决方案。谢谢。
您遇到的问题是
1.you are only calling one thread for the voice recognition function which only runs one time
- my second guess would be that these functions are not i/o bound functions
线程的工作方式是另一个函数启动,而前一个函数正在等待接收特定数据或类似的东西。如果没有这样的要求那么线程不是很有用
第二个函数开始的点 运行 是第一个函数等待接收某些资源的点。如果这不是您的要求,您可能需要查看多处理或 async/await 函数(使用 asyncio 包)
import asyncio
async def someFunc(event):
print('Interface change started') #for test
#creates Button_1
await event() #when code runs with voiceRecognition past as the event parameter, that is what will run here
#deletes config.Button_2 (from another func)
print('Interface change finished') #for test
def voiceRecognition ():
print('Voice recognition was started') #for test
r = sr.Recognizer()
with sr.Microphone(device_index=1) as sourse:
audio = r.listen(sourse)
query = r.recognize_google(audio)
print(query.lower())
print('Voice recognition was finished') #for test
asyncio.run(someFunc(voiceRecognition)) #to run it asynchronously
我真的不知道你的 class 是什么样子,所以我把它写成一个典型的函数,这意味着你必须稍微调整它以适应你的代码。
Notice the change to how your
voiceRecognition()
is called insomeFunc
using your event parameter and also notice the position event is now called at insomeFunc