Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved Multhearding using PyQt5

    General and Desktop
    2
    6
    1789
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dayama Pradeep
      Dayama Pradeep last edited by

      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 stopped

      please help me.

      thanks

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        Dayama Pradeep 1 Reply Last reply Reply Quote 0
        • Dayama Pradeep
          Dayama Pradeep @SGaist last edited by

          @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

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            How did you use moveToThread ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • Dayama Pradeep
              Dayama Pradeep last edited by

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

              https://stackoverflow.com/questions/6783194/background-thread-with-qthread-in-pyqt

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                What if you call quit in place of terminate ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post