使用 moveToThread 在 PyQt5 中启动 QThreads。一个线程无法正常启动
Launching QThreads in PyQt5 using moveToThread. One thread fails to launch properly
我在下面的(半)工作示例中有 4 个工作线程。我只能让前三个工作线程工作。如何让第 4 个线程到达 运行?
print(QThread.idealThreadCount())
returns '8' 在我的笔记本电脑上。
我可以对代码重新排序以组成 3 个工人的任意组合 运行。
from PyQt5.QtCore import QThread, QObject
from PyQt5.QtWidgets import QWidget
import sys
from PyQt5.QtWidgets import QApplication
import time
class A(QObject):
def run(self):
while 1:
print('A', time.ctime())
time.sleep(1)
class B(QObject):
def run(self):
while 1:
print('B', time.ctime())
time.sleep(1)
class C(QObject):
def run(self):
while 1:
print('C', time.ctime())
time.sleep(1)
class D(QObject):
def run(self):
while 1:
print('D', time.ctime())
time.sleep(1)
class window1(QWidget):
def __init__ (self, parent = None):
super().__init__ () #parent widget
print(QThread.idealThreadCount())
self.thread1 = QThread()
obj1 = A()
obj1.moveToThread(self.thread1)
self.thread1.started.connect(obj1.run)
self.thread1.start()
self.thread2 = QThread()
obj2 = B()
obj2.moveToThread(self.thread2)
self.thread2.started.connect(obj2.run)
self.thread2.start()
self.thread3 = QThread()
obj3 = C()
obj3.moveToThread(self.thread3)
self.thread3.started.connect(obj3.run)
self.thread3.start()
self.thread4 = QThread()
obj4 = D()
obj4.moveToThread(self.thread4)
self.thread4.started.connect(obj4.run)
self.thread4.start()
app = QApplication(sys.argv)
w = window1()
w.show()
sys.exit(app.exec_())
我发现如果我在 __init__
方法的最后添加一个 time.sleep(1)
延迟,所有线程现在都可以工作....
不过我想明白为什么。更好的答案将不胜感激。
from PyQt5.QtCore import QThread, QObject
from PyQt5.QtWidgets import QWidget, QApplication
import sys
import time
class A(QObject):
def run(self):
while 1:
print('A', time.ctime())
time.sleep(1)
class B(QObject):
def run(self):
while 1:
print('B', time.ctime())
time.sleep(1)
class C(QObject):
def run(self):
while 1:
print('C', time.ctime())
time.sleep(1)
class D(QObject):
def run(self):
while 1:
print('D', time.ctime())
time.sleep(1)
class window1(QWidget):
def __init__ (self, parent = None):
super().__init__ () #parent widget
self.thread1 = QThread()
obj1 = A()
obj1.moveToThread(self.thread1)
self.thread1.started.connect(obj1.run)
self.thread1.start()
self.thread2 = QThread()
obj2 = B()
obj2.moveToThread(self.thread2)
self.thread2.started.connect(obj2.run) #this sets up a signal in the other direction??
self.thread2.start()
self.thread3 = QThread()
obj3 = C()
obj3.moveToThread(self.thread3)
self.thread3.started.connect(obj3.run) #this sets up a signal in the other direction??
self.thread3.start()
self.thread4 = QThread()
obj4 = D()
obj4.moveToThread(self.thread4)
self.thread4.started.connect(obj4.run)
self.thread4.start()
time.sleep(1)
app = QApplication(sys.argv) #every pyqt application must create an application object
w = window1()
w.show()
sys.exit(app.exec_())
您没有存储对 obj1
、obj2
等的引用。因为它们没有父级(使用 moveToThread
需要父级),它们最终会被垃圾回收__init__
方法。您添加的 time.sleep(1)
只是延迟了 __init__
方法和垃圾收集的结束。
如果您存储对对象的引用(例如 self.obj1 = ...
),那么您的所有线程都应该 运行 正确。
我在下面的(半)工作示例中有 4 个工作线程。我只能让前三个工作线程工作。如何让第 4 个线程到达 运行?
print(QThread.idealThreadCount())
returns '8' 在我的笔记本电脑上。
我可以对代码重新排序以组成 3 个工人的任意组合 运行。
from PyQt5.QtCore import QThread, QObject
from PyQt5.QtWidgets import QWidget
import sys
from PyQt5.QtWidgets import QApplication
import time
class A(QObject):
def run(self):
while 1:
print('A', time.ctime())
time.sleep(1)
class B(QObject):
def run(self):
while 1:
print('B', time.ctime())
time.sleep(1)
class C(QObject):
def run(self):
while 1:
print('C', time.ctime())
time.sleep(1)
class D(QObject):
def run(self):
while 1:
print('D', time.ctime())
time.sleep(1)
class window1(QWidget):
def __init__ (self, parent = None):
super().__init__ () #parent widget
print(QThread.idealThreadCount())
self.thread1 = QThread()
obj1 = A()
obj1.moveToThread(self.thread1)
self.thread1.started.connect(obj1.run)
self.thread1.start()
self.thread2 = QThread()
obj2 = B()
obj2.moveToThread(self.thread2)
self.thread2.started.connect(obj2.run)
self.thread2.start()
self.thread3 = QThread()
obj3 = C()
obj3.moveToThread(self.thread3)
self.thread3.started.connect(obj3.run)
self.thread3.start()
self.thread4 = QThread()
obj4 = D()
obj4.moveToThread(self.thread4)
self.thread4.started.connect(obj4.run)
self.thread4.start()
app = QApplication(sys.argv)
w = window1()
w.show()
sys.exit(app.exec_())
我发现如果我在 __init__
方法的最后添加一个 time.sleep(1)
延迟,所有线程现在都可以工作....
不过我想明白为什么。更好的答案将不胜感激。
from PyQt5.QtCore import QThread, QObject
from PyQt5.QtWidgets import QWidget, QApplication
import sys
import time
class A(QObject):
def run(self):
while 1:
print('A', time.ctime())
time.sleep(1)
class B(QObject):
def run(self):
while 1:
print('B', time.ctime())
time.sleep(1)
class C(QObject):
def run(self):
while 1:
print('C', time.ctime())
time.sleep(1)
class D(QObject):
def run(self):
while 1:
print('D', time.ctime())
time.sleep(1)
class window1(QWidget):
def __init__ (self, parent = None):
super().__init__ () #parent widget
self.thread1 = QThread()
obj1 = A()
obj1.moveToThread(self.thread1)
self.thread1.started.connect(obj1.run)
self.thread1.start()
self.thread2 = QThread()
obj2 = B()
obj2.moveToThread(self.thread2)
self.thread2.started.connect(obj2.run) #this sets up a signal in the other direction??
self.thread2.start()
self.thread3 = QThread()
obj3 = C()
obj3.moveToThread(self.thread3)
self.thread3.started.connect(obj3.run) #this sets up a signal in the other direction??
self.thread3.start()
self.thread4 = QThread()
obj4 = D()
obj4.moveToThread(self.thread4)
self.thread4.started.connect(obj4.run)
self.thread4.start()
time.sleep(1)
app = QApplication(sys.argv) #every pyqt application must create an application object
w = window1()
w.show()
sys.exit(app.exec_())
您没有存储对 obj1
、obj2
等的引用。因为它们没有父级(使用 moveToThread
需要父级),它们最终会被垃圾回收__init__
方法。您添加的 time.sleep(1)
只是延迟了 __init__
方法和垃圾收集的结束。
如果您存储对对象的引用(例如 self.obj1 = ...
),那么您的所有线程都应该 运行 正确。