Python 为什么QThread只打印两个线程调用中的一个?
这只打印“嗨”而不是“你好”。为什么它不同时打印这两个呢?如何更改它以使其同时打印这两个文件?对Python 为什么QThread只打印两个线程调用中的一个?,python,pyqt,pyside,qthread,Python,Pyqt,Pyside,Qthread,这只打印“嗨”而不是“你好”。为什么它不同时打印这两个呢?如何更改它以使其同时打印这两个文件?对startThread的第二次调用将取消第一次调用。线程没有时间打印“hello”,您再次调用它,要求打印“hi”。 您可以在QThread.start()之后调用QThread.wait()以等待线程完成 工作示例(同样,具有正确标识): 此示例将在线程运行时冻结用户界面。 因此,对于更复杂的任务,您应该使用信号和插槽机制。对startThread的第二次调用将取消第一次调用。线程没有时间打印“he
startThread
的第二次调用将取消第一次调用。线程没有时间打印“hello”,您再次调用它,要求打印“hi”。
您可以在QThread.start()
之后调用QThread.wait()
以等待线程完成
工作示例(同样,具有正确标识):
此示例将在线程运行时冻结用户界面。
因此,对于更复杂的任务,您应该使用信号和插槽机制。对
startThread
的第二次调用将取消第一次调用。线程没有时间打印“hello”,您再次调用它,要求打印“hi”。
您可以在QThread.start()
之后调用QThread.wait()
以等待线程完成
工作示例(同样,具有正确标识):
此示例将在线程运行时冻结用户界面。
因此,对于更复杂的任务,您应该使用信号和插槽机制。这个示例非常小,因为线程只做一件事(打印字符串),然后退出。如果您确实希望并行启动两个线程,那么可以修改GG类以跟踪多个线程对象
class WorkerThread(QThread):
def _init_(self, mw):
super(WorkerThread, self)._init_(mw)
self.gameName = ""
def setGameName(self, currGameName):
self.gameName = currGameName
def run(self):
print self.gameName
class GG(object):
workerThread = WorkerThread()
def startThread(self,stringer):
self.workerThread.setGameName(stringer)
self.workerThread.start()
self.workerThread.wait()
harro = GG()
harro.startThread("hello")
harro.startThread("hi")
这个例子很小,因为线程只做一件事(打印字符串),然后退出。如果您确实希望并行启动两个线程,那么可以修改GG类以跟踪多个线程对象
class WorkerThread(QThread):
def _init_(self, mw):
super(WorkerThread, self)._init_(mw)
self.gameName = ""
def setGameName(self, currGameName):
self.gameName = currGameName
def run(self):
print self.gameName
class GG(object):
workerThread = WorkerThread()
def startThread(self,stringer):
self.workerThread.setGameName(stringer)
self.workerThread.start()
self.workerThread.wait()
harro = GG()
harro.startThread("hello")
harro.startThread("hi")
class WorkerThread(QThread):
def _init_(self, mw):
super(WorkerThread, self)._init_(mw)
self.gameName = ""
def setGameName(self, currGameName):
self.gameName = currGameName
def run(self):
print self.gameName
class GG(object):
workerThreads = []
def startThread(self,stringer):
self.workerThreads.append(WorkerThread())
self.workerThreads[-1].setGameName(stringer)
self.workerThreads[-1].start()
harro = GG()
harro.startThread("hello")
harro.startThread("hi")