【发布时间】:2020-11-12 10:44:13
【问题描述】:
我想每1s获取一次光谱仪的光谱,下面是我为此编写的代码。
但是,当光谱仪以低于 1 Hz 的频率触发时, 请求频谱的函数将等待触发并需要超过 1 秒的时间来处理,而计时器已经超时。 这会导致线程非常滞后并最终冻结。
有什么更好的方法来编写它,以便在函数完成自身后调用函数 'acquire_spectrum' 1s,而不是每 1s 调用一次?
class Spec_Thread(QThread):
def __init__(self):
QThread.__init__(self)
self.signals = Signals()
self.specth = Spectrometer.from_first_available() #connect to the spectrometer
self.threadtimer = QTimer()
self.threadtimer.moveToThread(self)
self.threadtimer.timeout.connect(self.acquire_spectrum)
def acquire_spectrum(self): #acquire the current spectrum from the spectrometer
print('in thread,', device_running)
if device_running == True:
self.specth.open() #open the usb portal
self.wavelengths = self.specth.wavelengths() #acquire wavelengths (will wait for a trigger)
self.intensities = self.specth.intensities() #acquire intensities (will wait for a trigger)
self.specth.close() #close usb portal
self.signals.new_spectrum.emit(self.wavelengths, self.intensities)
else:
print('Device stopped')
return
def run(self):
self.threadtimer.start(10000)
loop = QEventLoop()
loop.exec_()
【问题讨论】:
标签: python pyqt pyqt5 qthread qtimer