Multhearding using PyQt5
-
Hello,
Trying to experiment with PyQt5 Qthreads, where in trying with multi thread.
Below is the thread class//import sys import time from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtCore import (QCoreApplication, QObject, QRunnable, QThread, QThreadPool, pyqtSignal) # Subclassing QThread # http://qt-project.org/doc/latest/qthread.html class serial_thread(QThread): def __init__(self): super(serial_thread, self).__init__() self.TIME_INTERVAL = 5 self.ctimer = QTimer() self.direction = 'T' self.ctimer.timeout.connect(self.readWriteData) def readWriteData(self): if(self.direction == 'T'): self.direction = 'R' print('inside Transmit') elif(self.direction == 'R'): self.direction = 'T' print('inside Receive') def receive_info(self): print('inside Recieve info') self.start() def run(self): print('inside run') self.ctimer.start(self.TIME_INTERVAL) self.exec_() def stop(self): #self._isRunning = False self.terminate() self.wait() self.ctimer.stop() print('thread is been stopped')
from main theard. creating 2 instances of child theards
for i in range(2): self.serial_thread[i] = serial_thread()
when start is clicked from UI, thread starts, as shown below
self.serial_theard[index].receive_info()
when stop is clicked from UI, thread stops, as shown below
self.serial_theard[index].stop()
Issue: The thread is not been stopped once stop is clicked from UI. it goes in stop function but readWriteData function keeps executing
There are 2 tabs in UI, where in each tab is to associated with each thread object, the above issue happens only once 1 tab (thread)is started, and the same tab (thread )is closed or stoppedplease help me.
thanks
-
Hi,
Calling terminate is not a good idea like explained in the documentation. Since you are using the thread's event loop, you should call quit and then wait.
Also, you have several threads, are you sure you are stoping them all ?
-
How did you use moveToThread ?
-
well.. implemented in similar fashion as shown in below link
https://stackoverflow.com/questions/6783194/background-thread-with-qthread-in-pyqt
-
What if you call quit in place of terminate ?