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

PySide2 and PySide6 freezes when a QThread contains a PyAudio() stream, while PyQt5 works well

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Invalid
    • Icon: Not Evaluated Not Evaluated
    • None
    • 5.15.2
    • PySide
    • None
    • Windows Python3.8.0
    • Windows

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

      But Using PyQt5 with the same code won't meet any trouble.

      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):
              self.p = pyaudio.PyAudio()
              stream = self.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.

       Linux:

       apt-get install portaudio19-dev
      

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

            crmaurei Cristian Maureira-Fredes
            haujetzhao Haojie Zhao
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes