Important: Please read the Qt Code of Conduct -

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
    class serial_thread(QThread):  
       def __init__(self):
          super(serial_thread, self).__init__()
          self.TIME_INTERVAL = 5
          self.ctimer = QTimer() 
          self.direction = 'T'
       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')    
       def run(self):
          print('inside run')
       def stop(self):
          #self._isRunning = False
          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


    when stop is clicked from UI, thread stops, as shown below


    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 stopped

    please help me.


  • Lifetime Qt Champion


    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 ?

  • @SGaist Okay, Got it,. Actually tired a different way of multithreading using Qthreads.. and as of now it seems to be working.. I used move to thread.. and doing same.. as u suggested.. quit and wait

  • Lifetime Qt Champion

    How did you use moveToThread ?

  • well.. implemented in similar fashion as shown in below link

  • Lifetime Qt Champion

    What if you call quit in place of terminate ?

Log in to reply