I'm learning to use threads and GUIs, so please excuse me if I'm messing up the terminology.
I have an application where I'm monitoring a socket for data and doing some heavy processing before displaying the results in a GUI:
- thread1 to monitor socket for incoming data
- thread2 to process the data
- thread3 GUI thread
I'm passing data between the threads by emitting a signal. I want to know if my data processing thread can keep up with the incoming data.
From what I understand, when a signal is emitted, it is put into a queue for the connected slot. I haven't been able to find any information on how to check this queue length to see if it's getting backed up.
See below example:
- Data is generated every 0.5 seconds
- Data is processed every 1.0 seconds
Data is obviously getting backed up, but how do I get information on how many events still need to be processed?
from PyQt5.QtCore import QObject, QThread, QTimer
from PyQt5.QtCore import pyqtSlot, pyqtSignal
from PyQt5.QtWidgets import QMainWindow
import numpy as np
class Worker(QObject):
dataready = pyqtSignal(np.ndarray)
def __init__(self, timer_len=500):
super(Worker,self).__init__()
self.timer_len = timer_len
def run(self):
self.timer = QTimer()
self.timer.timeout.connect(self.work)
self.timer.start(self.timer_len)
def work(self):
print('received new data!')
data = np.random.rand(10)
self.dataready.emit(data)
if __name__ == "__main__":
import sys
import time
from PyQt5.QtWidgets import QApplication
app = QApplication([])
@pyqtSlot(np.ndarray)
def processdata(arr):
time.sleep(1.0) # process data every 1000ms
print(arr)
thread = QThread()
worker = Worker(timer_len=500) # generate new data every 500ms
worker.dataready.connect(processdata)
worker.moveToThread(thread)
thread.started.connect(worker.run)
thread.start()
sys.exit(app.exec_())