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?
-
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
toPySide6
also have the same problem, but replaceing thePySide2
toPyQt5
will run normally.Is this a bug or I had the wrong usage?
-
@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?