Python while循环在“try”之前不会执行
我在这里得到了一个非常简单的python while循环:Python while循环在“try”之前不会执行,python,multithreading,while-loop,Python,Multithreading,While Loop,我在这里得到了一个非常简单的python while循环: def run(self): while True: self.__main_thread.ask('wake',{}) time.sleep(0.25) 在我将代码更改为以下内容之前,不会执行“ask”功能: def run(self): while True: try: self.__main_thread.ask('wake',{})
def run(self):
while True:
self.__main_thread.ask('wake',{})
time.sleep(0.25)
在我将代码更改为以下内容之前,不会执行“ask”功能:
def run(self):
while True:
try:
self.__main_thread.ask('wake',{})
except:
print(sys.exc_info())
time.sleep(0.25)
这是一个线程类,我想做一些与主线程并行的工作
以下是“询问”和“唤醒”功能:
def ask(self, action, arguments):
if hasattr(self, action) and callable(getattr(self, action)):
try:
return getattr(self, action)(arguments)
except:
self.__exception_queue.put(sys.exc_inf())
else:
error = {'errCode':'ERR_00',"logMsg": "Invalid action: requested action '%s' is not implemented." % action}
print (error)
return error
def wake(self):
pic=None
try:
frame = self.webcam.read()[1]
frame = cv2.resize(frame, (300, 300,interpolation=cv2.INTER_CUBIC)
pic = cv2.imencode('.png',frame)[1]
except:
if self.debug:
print ('no picture available')
raise
else:
self._photo_b64 = base64.encodestring(pic)
基本上,我正在尝试使用一个线程周期性地从我的网络摄像头读取数据。
如果我将“try”和“except”添加到“run”功能中,网络摄像头图像将按预期更新,否则,不会从网络摄像头读取图像,也不会引发异常。问怎么办?它是否因为任何原因而阻塞?为什么说它没有被执行?ask函数检查所需的ask方法是否由对象实现。如果是,调用该方法。当while循环正常执行时,我从我的网络摄像头获得了更新的图片。另外,我什么都没有。这就是我知道while循环是否被执行的方式。为什么不在循环中添加一些打印,这样你就可以看到它正在被执行。