Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PySide2 and PySide6 freezes when a QThread contains a PyAudio() stream, while PyQt5 works well. Is that a bug or I had the wrong usage?
Forum Update on Monday, May 27th 2025

PySide2 and PySide6 freezes when a QThread contains a PyAudio() stream, while PyQt5 works well. Is that a bug or I had the wrong usage?

Scheduled Pinned Locked Moved Unsolved Qt for Python
pyside2qt for pythonpython
2 Posts 2 Posters 1.0k Views
  • 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.
  • H Offline
    H Offline
    Haujet Zhao
    wrote on last edited by
    #1

    When using PySide2 or PySide6, opening a PyAudio stream in a QThread, many actions relating to GUI and webbrowser will result the GUI freezing.

    But Using PyQt5 with the same code won't meet any trouble. I wonder is this a bug of PySide?

    I'm running it on Windows10, using PySide2(5.15.0), PySide6(6.0.0), PyQt5(5.15.0).

    Here is the code:

    import sys, pyaudio
    from PySide2.QtCore import *
    from PySide2.QtWidgets import *
    from PySide2.QtGui import *
    import threading, webbrowser
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            widget = Widget()
            self.setCentralWidget(widget)
            self.show()
    
    class Widget(QWidget):
        def __init__(self):
            super().__init__()
            self.btn1 = QPushButton('''Open Being''')
            self.btn1.clicked.connect(lambda: webbrowser.open('https://www.bing.com'))
            self.btn2 = QPushButton('''Get pyaudio''')
            self.btn2.clicked.connect(self.getPyaudioThread)
            layout = QVBoxLayout()
            layout.addWidget(self.btn1)
            layout.addWidget(self.btn2)
            self.setLayout(layout)
    
        def getPyaudioThread(self):
            self.pyaudioThread = PyaudioThread()
            self.pyaudioThread.start()
    
    class PyaudioThread(QThread):
        CHUNK = 1024
        FORMAT = pyaudio.paInt16
        CHANNELS = 1
        RATE = 16000
    
        def __init__(self):
            super().__init__()
    
        def record(self, stream):
            while True:
                stream.read(self.CHUNK)
    
        def run(self):
            p = pyaudio.PyAudio()
            stream = p.open(channels=self.CHANNELS,
                            format=self.FORMAT,
                            rate=self.RATE,
                            input=True,
                            frames_per_buffer=self.CHUNK)
            # threading.Thread(target=self.record, args=[stream]).start()
            print('pyaudio required')
    
    def main():
        app = QApplication(sys.argv)
        mainWindow = MainWindow()
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    

    After running it, first click "Get pyaudio" button, then click "Open Being" button, then the whole GUI will freeze.

    Replacing the PySide2 to PySide6 also have the same problem, but replaceing the PySide2 to PyQt5 will run normally.

    Is this a bug or I had the wrong usage?

    JonBJ 1 Reply Last reply
    0
    • H Haujet Zhao

      When using PySide2 or PySide6, opening a PyAudio stream in a QThread, many actions relating to GUI and webbrowser will result the GUI freezing.

      But Using PyQt5 with the same code won't meet any trouble. I wonder is this a bug of PySide?

      I'm running it on Windows10, using PySide2(5.15.0), PySide6(6.0.0), PyQt5(5.15.0).

      Here is the code:

      import sys, pyaudio
      from PySide2.QtCore import *
      from PySide2.QtWidgets import *
      from PySide2.QtGui import *
      import threading, webbrowser
      
      class MainWindow(QMainWindow):
          def __init__(self):
              super().__init__()
              widget = Widget()
              self.setCentralWidget(widget)
              self.show()
      
      class Widget(QWidget):
          def __init__(self):
              super().__init__()
              self.btn1 = QPushButton('''Open Being''')
              self.btn1.clicked.connect(lambda: webbrowser.open('https://www.bing.com'))
              self.btn2 = QPushButton('''Get pyaudio''')
              self.btn2.clicked.connect(self.getPyaudioThread)
              layout = QVBoxLayout()
              layout.addWidget(self.btn1)
              layout.addWidget(self.btn2)
              self.setLayout(layout)
      
          def getPyaudioThread(self):
              self.pyaudioThread = PyaudioThread()
              self.pyaudioThread.start()
      
      class PyaudioThread(QThread):
          CHUNK = 1024
          FORMAT = pyaudio.paInt16
          CHANNELS = 1
          RATE = 16000
      
          def __init__(self):
              super().__init__()
      
          def record(self, stream):
              while True:
                  stream.read(self.CHUNK)
      
          def run(self):
              p = pyaudio.PyAudio()
              stream = p.open(channels=self.CHANNELS,
                              format=self.FORMAT,
                              rate=self.RATE,
                              input=True,
                              frames_per_buffer=self.CHUNK)
              # threading.Thread(target=self.record, args=[stream]).start()
              print('pyaudio required')
      
      def main():
          app = QApplication(sys.argv)
          mainWindow = MainWindow()
          sys.exit(app.exec_())
      
      if __name__ == '__main__':
          main()
      

      After running it, first click "Get pyaudio" button, then click "Open Being" button, then the whole GUI will freeze.

      Replacing the PySide2 to PySide6 also have the same problem, but replaceing the PySide2 to PyQt5 will run normally.

      Is this a bug or I had the wrong usage?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Haujet-Zhao
      I don't know whether it's "supposed" to work in PySide. PyQt5 is more robust/has more support for other libraries. Have a read anyway of the (only) reply to https://stackoverflow.com/questions/63502837/pyqt5-app-using-timer-to-run-a-function-with-a-loop-makes-it-freeze-up, as possible way forward for PySide2/6?

      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