0

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_())
  • 1
    You should not use sleep in the main thread, in your case processdata is in the main thread – eyllanesc Aug 06 '18 at 06:55
  • 1
    There is no way of knowing how many data can be in the queue, the signal will be sent whenever it is necessary, that is handled by the Qt eventloop. – eyllanesc Aug 06 '18 at 06:57
  • Hi, the sleep in the example is just to simulate if processing the data takes longer than the creation rate. If there's no way to know how much data is available, how can I analyze if processing will keep up in realtime? Thanks! – user2545167 Aug 06 '18 at 08:03
  • just wait to be done, if your task takes more time than you get the data then your design is incorrect but that depends on what you want to do, now I do not know what you are doing, in general I would propose to create a new thread if none of the threads already created are unoccupied. – eyllanesc Aug 06 '18 at 08:06
  • you say: *thread3 GUI thread*, the GUI must be in the thread by default, you should not be able to move it. – eyllanesc Aug 06 '18 at 08:07
  • I'm basically trying to figure out if the processing thread runs slower than data generation. Should I just time the loop? Before I moved to QThread, I would use python's built in threading + queues. I would just check queue.qsize() to see if there's a backup occuring... – user2545167 Aug 06 '18 at 08:17
  • If you are more famialized with threading, use it. QThread is just a generic way to handle native threads. the signals have no notion of how much data there is in the queue, the data queue is handled internally by Qt. – eyllanesc Aug 06 '18 at 08:19

0 Answers0