gpt4 book ai didi

python - QThreadPool 是按顺序运行而不是并行运行,是什么导致我的代码线程不并行运行?

转载 作者:太空宇宙 更新时间:2023-11-04 00:02:36 28 4
gpt4 key购买 nike

我想并行运行 pyqt5 QThreads,但我的代码似乎按顺序运行,有人能告诉我如何并行运行 QThreads 吗?

我的代码的输出:我预计它是并行运行的,而不是按顺序运行的。

Multithreading with maximum 4 threads
You pressed the Test button
Job 1
Job 2
Job 3
Job 4
Done.
THREAD COMPLETE!

代码:

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

import time
import traceback, sys

uart_result = ['1','2', '3', '4', '5', '6']

#Running all these methods in parallel
@pyqtSlot()
def run1():
print("Job 1")
return uart_result

@pyqtSlot()
def run2():
print("Job 2")
return uart_result

@pyqtSlot()
def run3():
print("Job 3")
return uart_result

@pyqtSlot()
def run4():
print("Job 4")
return uart_result

class WorkerSignals(QObject):
finished = pyqtSignal()
error = pyqtSignal(tuple)
result = pyqtSignal(object)
list = pyqtSignal(list)
progress = pyqtSignal(int)


class Worker(QRunnable):

def __init__(self, fn, *args, **kwargs):
super(Worker, self).__init__()

# Store constructor arguments (re-used for processing)
self.fn = fn
self.args = args
self.kwargs = kwargs
self.signals = WorkerSignals()

# Add the callback to our kwargs
self.kwargs['progress_callback'] = self.signals.progress

@pyqtSlot()
def run(self):
'''
Initialise the runner function with passed args, kwargs.
'''

# Retrieve args/kwargs here; and fire processing using them
try:
result = self.fn(*self.args, **self.kwargs)
except:
traceback.print_exc()
exctype, value = sys.exc_info()[:2]
self.signals.error.emit((exctype, value, traceback.format_exc()))
else:
self.signals.result.emit(result) # Return the result of the processing
finally:
self.signals.finished.emit() # Done



class MainWindow(QMainWindow):

def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
layout = QVBoxLayout()
b = QPushButton("START!")
b.pressed.connect(self.runner)
layout.addWidget(b)
w = QWidget()
w.setLayout(layout)
self.setCentralWidget(w)
self.show()

self.threadpool = QThreadPool()
print("Multithreading with maximum %d threads" % self.threadpool.maxThreadCount())


def execute_this_fn(self, progress_callback):
command_list=[run1, run2, run3, run4]
for i in range(4):
command_list[i]()

return "Done."
#this not printing the global list.
def print_output(self, uart_list):
print(uart_list)

def thread_complete(self):
print("THREAD COMPLETE!")

def runner(self):
print("You pressed the Test button")
# Pass the function to execute
worker = Worker(self.execute_this_fn) # Any other args, kwargs are passed to the run function
worker.signals.result.connect(self.print_output)
worker.signals.finished.connect(self.thread_complete)

# Execute
self.threadpool.start(worker)

app = QApplication([])
window = MainWindow()
app.exec_()

最佳答案

您必须为每个任务创建一个 Worker (QRunnable),在您的情况下,您只为其创建了一个,任务按顺序执行:

from PyQt5 import QtCore, QtGui, QtWidgets

import time
import traceback, sys

uart_result = ['1','2', '3', '4', '5', '6']

#Running all these methods in parallel
@QtCore.pyqtSlot()
def run1():
print("Job 1")
return uart_result

@QtCore.pyqtSlot()
def run2():
print("Job 2")
return uart_result

@QtCore.pyqtSlot()
def run3():
print("Job 3")
return uart_result

@QtCore.pyqtSlot()
def run4():
print("Job 4")
return uart_result

class WorkerSignals(QtCore.QObject):
finished = QtCore.pyqtSignal()
error = QtCore.pyqtSignal(tuple)
result = QtCore.pyqtSignal(object)
progress = QtCore.pyqtSignal(int)


class Worker(QtCore.QRunnable):
def __init__(self, fn, *args, **kwargs):
super(Worker, self).__init__()
self.fn = fn
self.args = args
self.kwargs = kwargs
self.signals = WorkerSignals()
def run(self):
try:
result = self.fn(*self.args, **self.kwargs)
except:
traceback.print_exc()
exctype, value = sys.exc_info()[:2]
self.signals.error.emit((exctype, value, traceback.format_exc()))
else:
self.signals.result.emit(result) # Return the result of the processing
finally:
self.signals.finished.emit() # Done



class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
b = QtWidgets.QPushButton("START!")
b.pressed.connect(self.runner)

w = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout(w)
layout.addWidget(b)
self.setCentralWidget(w)

def print_output(self, uart_list):
print(uart_list)

def thread_complete(self):
print("THREAD COMPLETE!")

def runner(self):
thread_pool = QtCore.QThreadPool.globalInstance()
print("Multithreading with maximum %d threads" % thread_pool.maxThreadCount())
print("You pressed the Test button")
for task in (run1, run2, run3, run4):
worker = Worker(task)
worker.signals.result.connect(self.print_output)
worker.signals.finished.connect(self.thread_complete)
thread_pool.start(worker)

if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

关于python - QThreadPool 是按顺序运行而不是并行运行,是什么导致我的代码线程不并行运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55211402/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com