from PySide2.QtCore import QThread, QThreadPool, QRunnable
from PySide2.QtWidgets import QApplication
import time
class Task(QRunnable):
def __init__(self, a, b):
super().__init__()
self.a = a
self.b = b
def run(self):
print(f"hello world from {QThread.currentThread()}")
ret = self.a + self.b
print(f"a+b={ret}")
for i in range(ret):
print(i)
time.sleep(0.5)
if __name__ == '__main__':
app = QApplication()
task1 = Task(1,2)
# 从线程池中取一个线程去执行任务
QThreadPool.globalInstance().start(task1)
task2 = Task(2,3)
# 从线程池中取一个线程去执行任务
QThreadPool.globalInstance().start(task2)
app.exec_()