Plotting real time data causes graph to freeze

82 views
Skip to first unread message

Keiji

unread,
Aug 26, 2018, 1:09:16 AM8/26/18
to pyqtgraph
Hi there. I know this question has been asked many times and a possible solution is using a timer (QTimer), but in my case it doesn't work. What I'm doing wrong?

I've also tried to increase the timer step (self.timer.start(200)) and move the processing part of the code to a new "worker" thread, communicating by signals with the UI thread.
Here's a simple version of my code:

from PyQt4 import QtGui, QtCore
import multiprocessing, Queue, time, pyqtgraph, sys

#Main window
import Plotter

def Input(queue):
    t = time.clock()
    queue.put(t)

def Output(queue, v):
    v = queue.get()

# -----------------------------------------------------------------
class UI_Thread(QtGui.QMainWindow, Plotter.Ui_MainWindow):
    Start_Worker = QtCore.pyqtSignal()

    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.QOn.clicked.connect(self.Calc)
        self.q = Queue.Queue(0) #FIFO Constructor
        self.v = 0       
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.Update)
       
    def Update(self):
   
    p1 = multiprocessing.Process(target=Input, args=(self.q,))
        p1.start()
        p1.join()
       
    p2 = multiprocessing.Process(target=Output, args=(self.q, self.v))
        p2.start()
        p2.join()
       
        self.Texto.setText(str(self.v))
       
    def Calc(self):
        self.timer.start(200)
       
       
# -----------------------------------------------------------------
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    PID = UI_Thread()
    PID.show()
    app.exec_()

Patrick

unread,
Aug 26, 2018, 10:42:58 PM8/26/18
to pyqtgraph
Hi,

I've used separate threads to acquire data and then notify the Qt UI thread using the signals. Demo code for that method here:

Someone else was trying to use a multiprocessing approach and I gave a working example of that here:

The issue is you want to start the processes from outside of the Qt event loop -- the process .join() commands will block until the new process ends, and so will block the Qt event loop. See example above, where the Qt event loop (app) is started from inside the receiver process, and the sender and receiver processes are started in "main" code block.

Patrick
Reply all
Reply to author
Forward
0 new messages