[英]Python and PyQt5 Overlap of parallel processing functions
I am working on a program that contains parallel processing, where it has to execute two tasks in parallel, and also every task must also execute two tasks in parallel, but when I run the program in the first when when executing one task the program works fine, but when executing two tasks there is Overlapping information.我正在开发一个包含并行处理的程序,它必须并行执行两个任务,并且每个任务还必须并行执行两个任务,但是当我在执行一个任务时第一次运行程序时,程序可以工作很好,但是在执行两个任务时会出现重叠信息。 Is there a solution to this problem?
这个问题有解决方案吗?
Code:代码:
class thread_X_1(QThread):
signal = pyqtSignal(np.ndarray)
def __init__(self):
super().__init__()
var = Window()
class thread_X_2(QThread):
signal = pyqtSignal(np.ndarray)
def __init__(self):
super().__init__()
var = Window()
class Window():
def __init__(self):
super().__init__()
self.pX_1 = thread_X_1()
self.pX_2 = thread_X_2()
self.pX_1.start()
self.pX_2.start()
self.pX_1.quit()
self.pX_2.quit()
self.pX_1.wait()
self.pX_2.wait()
class thread_X(QThread):
signal = pyqtSignal(np.ndarray)
def __init__(self):
super().__init__()
var = Window()
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.p1 = thread_X()
self.p1.start()
self.p2 = thread_X()
self.p2.start()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
What happens to me, is that when Qthread1 and Qthread2 are activated, in the normal case, Qthread1 should return Qthread1_1 and Qthread2_2, but this does not happen to me, but rather that Q1 returns Qthread1_1, Qthread1_2, Qthread2_1, Qthread2_2.发生在我身上的是,当Qthread1和Qthread2被激活时,在正常情况下,Qthread1应该返回Qthread1_1和Qthread2_2,但这不会发生在我身上,而是Q1返回Qthread1_1,Qthread1_2,Qthread2_1,Qthread2_2。
I think your code is not working as you want because in each call to thread_X() you are calling thread_X_1() and thread_X_2() in each one so as a result you are getting Qthread1_1, Qthread1_2, Qthread2_1, Qthread2_2.我认为您的代码没有按您的意愿工作,因为在每次调用 thread_X() 时,您都在调用 thread_X_1() 和 thread_X_2(),因此您得到 Qthread1_1、Qthread1_2、Qthread2_1、Qthread2_2。
You can checkout that in this code:您可以在此代码中签出:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout, QHBoxLayout, QLabel, QComboBox, QPushButton
from PyQt5.QtCore import QThread, pyqtSignal
import numpy as np
class thread_X_1(QThread):
signal = pyqtSignal(np.ndarray)
def __init__(self,num):
super().__init__()
print(num,"1")
class thread_X_2(QThread):
signal = pyqtSignal(np.ndarray)
def __init__(self,num):
super().__init__()
print(num,"2")
class Window():
def __init__(self,num):
super().__init__()
self.pX_1 = thread_X_1(num)
self.pX_2 = thread_X_2(num)
self.pX_1.start()
self.pX_2.start()
self.pX_1.quit()
self.pX_2.quit()
# self.pX_1.wait()
# self.pX_2.wait()
class thread_X(QThread):
signal = pyqtSignal(np.ndarray)
def __init__(self, num):
super().__init__()
var = Window(num)
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.p1 = thread_X(1)
self.p1.start()
self.p2 = thread_X(2)
self.p2.start()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
So probably the solution you are looking for is this where you check the thread you are going to use:因此,您正在寻找的解决方案可能是您检查要使用的线程的地方:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout, QHBoxLayout, QLabel, QComboBox, QPushButton
from PyQt5.QtCore import QThread, pyqtSignal
import numpy as np
class thread_X_1(QThread):
signal = pyqtSignal(np.ndarray)
def __init__(self,thread_number):
super().__init__()
print(thread_number,"1")
#var = Window()
class thread_X_2(QThread):
signal = pyqtSignal(np.ndarray)
def __init__(self,thread_number):
super().__init__()
print(thread_number,"2")
#var = Window()
class Window():
def __init__(self,thread_number):
super().__init__()
if(thread_number==1):
self.pX_1 = thread_X_1(thread_number)
self.pX_1.start()
self.pX_1.quit()
self.pX_1.wait()
if(thread_number==2):
self.pX_2 = thread_X_2(thread_number)
self.pX_2.start()
self.pX_2.quit()
self.pX_2.wait()
class thread_X(QThread):
signal = pyqtSignal(np.ndarray)
def __init__(self, thread_number):
super().__init__()
var = Window(thread_number)
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.p1 = thread_X(1) #Num of the thread
self.p1.start()
self.p2 = thread_X(2) #Num of the thread
self.p2.start()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
Tell me if this solution doesn't work for you.告诉我此解决方案是否不适合您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.