Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-2901

Call to function connected to Signal from QThread is executed in a different thread than the main thread.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • Not Evaluated
    • None
    • 6.8.0.1
    • PySide
    • None
    • Windows
    • 6b35ebfa0 (dev), 35eb8b218 (dev), be01f9aa2 (6.8), 074f46f37 (6.8)

    Description

      I am using a {{QThread to handle background operations in a parallel thread, and I use Qt signals to ensure that updates related to the UI are processed in the main thread, as required by Qt.}}

      Since the last update, I’ve encountered an issue where the method reconnect_one is failing because it’s being executed in a non-main thread. This is problematic because certain operations, particularly those interacting with the UI, must occur in the main thread.

      Here’s a simplified version of the code I'm working with:

      class threadCheckConnection(QObject):
          
          finished = Signal()
          sqs_checker = Signal()
          refresh_data = Signal()
          
          def __init__(self, lock, parent):
              super().__init__()
              self.lock = threading.Lock()  # Threading lock to handle thread safety
              self.Parent = parent    def run(self):
              # Simulating connection checking logic
              if verificar_conexion_internet():
                  if not Global.INTERNET_CONNECTION:
                      logging.info('Se ha recuperado la conexión a Internet')
                  Global.INTERNET_CONNECTION = True
                  
                  if not Global.BD_CONNECTION:
                      try:
                          Global.BD_CONNECTION = DBManager.init_connection()
                      except:
                          Global.BD_CONNECTION = None
                              
                      if Global.BD_CONNECTION: 
                          self.refresh_data.emit()  # Emitting signal to refresh data
                      
              else:
                  if Global.INTERNET_CONNECTION:
                      logging.error('Se ha perdido la conexión a Internet')
                  Global.BD_CONNECTION = False
                  Global.INTERNET_CONNECTION = False
                  DBManager.close()        
                  
              self.finished.emit()  # Emitting finished 
      
      
      self.objThread_check_connection = QThread() 
      self.obj_check_connection = threadCheckConnection(self.reconnect_lock, self)
      self.obj_check_connection.moveToThread(self.objThread_check_connection)
      self.obj_check_connection.finished.connect(self.objThread_check_connection.quit, Qt.ConnectionType.QueuedConnection)
      self.obj_check_connection.refresh_data.connect(self.reconnect_one, Qt.ConnectionType.QueuedConnection)
      self.objThread_check_connection.started.connect(self.obj_check_connection.run)
      
      
       
      {{{}{}}}Here, {{refresh_data}} is connected to the {{reconnect_one}} method, which triggers when there is an internet reconnection. The {{reconnect_one}} method handles UI updates and other critical operations that need to run on the main thread.
      The problem arises because the following code confirms that {{reconnect_one}} is running in a non-main thread:
      
       
      
      @cursor_wait
      def reconnect_one(self):
          # Check the current thread
          current_thread = QThread.currentThread()
          print(f"Hilo actual: \{current_thread}")
          
          # Check if it's the main thread
          main_thread = QCoreApplication.instance().thread()
          if current_thread == main_thread:
              print("This is the main thread.") 
          else:
              print("This isn’t the main thread."

      This diagnostic shows that reconnect_one is executed in a different thread, even though it’s connected via Qt.ConnectionType.QueuedConnection, which should ensure that the slot is executed in the context of the main thread.

      The expected behavior is for the function to run in the main thread because it updates the UI and makes essential configurations. However, it is currently being executed in a background thread. This problem surfaced after a recent update, and the functionality was working as expected prior to this.

      Attachments

        1. pyside2901.py
          2 kB
        2. pyside2901_pyqt.py
          2 kB
        3. pyside2901_log.txt
          2 kB
        4. pyside2901_cpp.zip
          1 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            crmaurei Cristian Maureira-Fredes
            gcastro1979 Gonzalo Castro Vázquez
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes