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

QTcpSocket::waitForXXX() executed in a secondary thread block the main thread

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 5.15.4
    • 5.15.4, 6.1.1
    • PySide
    • None
    • c7c0d1527085b4038ee39c66910285a83a110735 (pyside/pyside-setup/dev) f417d1b85d8f2bd499f18091994baab126dacc66 (pyside/pyside-setup/6.1) 879eb2087a1395201dd58540ca09a015abfb133d (pyside/tqtc-pyside-setup/5.15)

    Description

      When the waitForXXX blocking function is executed in a secondary thread, the main thread is also blocked. In the following example the time is printed every T seconds but when the waitForRead method is executed in a secondary thread it blocks the main eventloop so the QTimer is no longer fired.

      from PySide6 import QtCore, QtNetwork
      
      
      class Runnable(QtCore.QRunnable):
          def run(self):
              socket = QtNetwork.QTcpSocket()
              socket.connectToHost("example.com", 80)
              if not socket.waitForConnected(5 * 1000):
                  print("Not Connected")
                  return
              print("Connected")
      
              if not socket.waitForReadyRead(10 * 1000):
                  print("Timeout")
                  return
      
              print("Answer received")
      
      
      def handle_timeout():
          dt = QtCore.QDateTime.currentDateTime()
          print(dt.toString())
      
      
      def main():
          app = QtCore.QCoreApplication()
      
          timer = QtCore.QTimer(interval=100, timeout=handle_timeout)
          timer.start()
      
          runnable = Runnable()
          QtCore.QThreadPool.globalInstance().start(runnable)
      
          QtCore.QTimer.singleShot(15 * 1000, app.quit)
      
          app.exec()
      
      
      if __name__ == "__main__":
          main()
      

      The bug is observed with PySide2 and PySide6 but in PyQt it works correctly.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            crmaurei Cristian Maureira-Fredes
            eyllanesc Edwin Christian Yllanes Cucho
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes