Last active
January 4, 2018 14:19
multiprocessing gui example
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 | |
from PyQt5 import QtWidgets | |
def calc(): | |
# random cpu bound computation | |
result = 0 | |
for i in range(10 ** 7): | |
result += i ** 2 | |
return result | |
if __name__ == "__main__": | |
app = QtWidgets.QApplication(sys.argv) | |
w = QtWidgets.QListWidget() | |
w.show() | |
# populate without blocking using all your cores! | |
pool = multiprocessing.Pool() | |
for _ in range(32): | |
pool.apply_async(calc, callback=lambda x: w.addItem(str(x))) | |
app.exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment