Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Multhearding using PyQt5
Forum Updated to NodeBB v4.3 + New Features

Multhearding using PyQt5

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 2.2k Views 2 Watching
  • 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 PradeepD Offline
    Dayama PradeepD Offline
    Dayama Pradeep
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 PradeepD 1 Reply Last reply
      0
      • SGaistS SGaist

        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 ?

        Dayama PradeepD Offline
        Dayama PradeepD Offline
        Dayama Pradeep
        wrote on last edited by
        #3

        @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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0
          • Dayama PradeepD Offline
            Dayama PradeepD Offline
            Dayama Pradeep
            wrote on last edited by
            #5

            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
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved