Created
August 14, 2018 05:26
-
-
Save seanwu1105/9b98d59c150d71f434a07f7b93886b68 to your computer and use it in GitHub Desktop.
PySide2 QThread Issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PyQt5.QtCore import QThread | |
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, | |
QComboBox, QWidget, QVBoxLayout) | |
class Task(QThread): | |
def run(self): | |
print('task started') | |
k = 0 | |
for i in range(10000): | |
for j in range(50000): | |
k += 1 | |
print('task finished') | |
class Gui(QMainWindow): | |
def __init__(self): | |
super().__init__() | |
base = QWidget() | |
layout = QVBoxLayout() | |
button = QPushButton('click me') | |
button.clicked.connect(self.do_task) | |
combobox = QComboBox() | |
combobox.addItems(['1', '2', '3', '4', '5']) | |
layout.addWidget(button) | |
layout.addWidget(combobox) | |
base.setLayout(layout) | |
self.setCentralWidget(base) | |
def do_task(self): | |
self.thread = Task() | |
self.thread.start() | |
def main(): | |
app = QApplication(sys.argv) | |
window = Gui() | |
window.show() | |
sys.exit(app.exec_()) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import multiprocessing as mp | |
from PyQt5.QtCore import QThread | |
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, | |
QComboBox, QWidget, QVBoxLayout) | |
class Task(QThread): | |
def run(self): | |
print('task started') | |
with mp.Pool() as pool: | |
res = pool.map(mp_task, range(10000)) | |
print('task finished') | |
def mp_task(x): | |
ret = 0 | |
for i in range(x + 50000): | |
ret += i | |
return ret | |
class Gui(QMainWindow): | |
def __init__(self): | |
super().__init__() | |
base = QWidget() | |
layout = QVBoxLayout() | |
button = QPushButton('click me') | |
button.clicked.connect(self.do_task) | |
combobox = QComboBox() | |
combobox.addItems(['1', '2', '3', '4', '5']) | |
layout.addWidget(button) | |
layout.addWidget(combobox) | |
base.setLayout(layout) | |
self.setCentralWidget(base) | |
def do_task(self): | |
self.thread = Task() | |
self.thread.start() | |
def main(): | |
app = QApplication(sys.argv) | |
window = Gui() | |
window.show() | |
sys.exit(app.exec_()) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PySide2.QtCore import QThread | |
from PySide2.QtWidgets import (QApplication, QMainWindow, QPushButton, | |
QComboBox, QWidget, QVBoxLayout) | |
class Task(QThread): | |
def run(self): | |
print('task started') | |
k = 0 | |
for i in range(10000): | |
for j in range(50000): | |
k += 1 | |
print('task finished') | |
class Gui(QMainWindow): | |
def __init__(self): | |
super().__init__() | |
base = QWidget() | |
layout = QVBoxLayout() | |
button = QPushButton('click me') | |
button.clicked.connect(self.do_task) | |
combobox = QComboBox() | |
combobox.addItems(['1', '2', '3', '4', '5']) | |
layout.addWidget(button) | |
layout.addWidget(combobox) | |
base.setLayout(layout) | |
self.setCentralWidget(base) | |
def do_task(self): | |
self.thread = Task() | |
self.thread.start() | |
def main(): | |
app = QApplication(sys.argv) | |
window = Gui() | |
window.show() | |
sys.exit(app.exec_()) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import multiprocessing as mp | |
from PySide2.QtCore import QThread | |
from PySide2.QtWidgets import (QApplication, QMainWindow, QPushButton, | |
QComboBox, QWidget, QVBoxLayout) | |
class Task(QThread): | |
def run(self): | |
print('task started') | |
with mp.Pool() as pool: | |
res = pool.map(mp_task, range(10000)) | |
print('task finished') | |
def mp_task(x): | |
ret = 0 | |
for i in range(x + 50000): | |
ret += i | |
return ret | |
class Gui(QMainWindow): | |
def __init__(self): | |
super().__init__() | |
base = QWidget() | |
layout = QVBoxLayout() | |
button = QPushButton('click me') | |
button.clicked.connect(self.do_task) | |
combobox = QComboBox() | |
combobox.addItems(['1', '2', '3', '4', '5']) | |
layout.addWidget(button) | |
layout.addWidget(combobox) | |
base.setLayout(layout) | |
self.setCentralWidget(base) | |
def do_task(self): | |
self.thread = Task() | |
self.thread.start() | |
def main(): | |
app = QApplication(sys.argv) | |
window = Gui() | |
window.show() | |
sys.exit(app.exec_()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment