qt - qt PyQt: qthread通过信号中断

  显示原文与译文双语对照的内容

我非常新的,所以我希望在我所尝试的内容中不会有任何奇怪的事情。 我试图使用PyQt信号在QThreads之间创建一个交互。 特别是,我要做的是从线程发出信号,这个信号应该中断线程运行特定方法的内容。 顺便说一下,我有一些问题,所以我想知道我想要做什么是合法的。

例如现在我正在尝试执行这样的操作:


import sys


import time


from PyQt4 import QtGui as qt


from PyQt4 import QtCore as qtcore


from PyQt4.QtCore import QThread


import threading



app = qt.QApplication(sys.argv)


class widget(qt.QWidget):



 def __init__(self, parent=None):


 qtcore.QObject.__init__(self)



 def appinit(self):


 self.mysignal = qtcore.SIGNAL("mysignal")


 thread = mythread(self)


 thread.start()


 time.sleep(5)


 print"before emit",str(threading.current_thread())


 self.emit(self.mysignal,"hello, I'm thread"+str(threading.current_thread()))


 print"after emit",str(threading.current_thread())



class mythread(QThread):


 def __init__(self,parent):


 qtcore.QThread.__init__(self, parent=app)


 self.parent=parent



 def run(self):


 self.mysignal = qtcore.SIGNAL("mysignal")


 self.connect(self.parent, self.mysignal, self.myfunc)


 for i in range(15):


 print"**",threading.current_thread(),i


 time.sleep(1)



 def myfunc(self, msg):


 print threading.current_thread(), msg,"Enter"


 time.sleep(5)


 print threading.current_thread(), msg,"Exit"



def main():


 mywidget = widget()


 mywidget.show()


 qtcore.QTimer.singleShot(0, mywidget.appinit)


 sys.exit(app.exec_())



main()



我得到的输出是:


** <_DummyThread(Dummy-1, started daemon 1968)> 0


** <_DummyThread(Dummy-1, started daemon 1968)> 1


** <_DummyThread(Dummy-1, started daemon 1968)> 2


** <_DummyThread(Dummy-1, started daemon 1968)> 3


** <_DummyThread(Dummy-1, started daemon 1968)> 4


before emit <_MainThread(MainThread, started 2928)>


<_MainThread(MainThread, started 2928)> hello, I'm thread <_MainThread(MainThread, started 2928)> Enter


** <_DummyThread(Dummy-1, started daemon 1968)> 5


** <_DummyThread(Dummy-1, started daemon 1968)> 6


** <_DummyThread(Dummy-1, started daemon 1968)> 7


** <_DummyThread(Dummy-1, started daemon 1968)> 8


** <_DummyThread(Dummy-1, started daemon 1968)> 9


<_MainThread(MainThread, started 2928)> hello, I'm thread <_MainThread(MainThread, started 2928)> Exit


after emit <_MainThread(MainThread, started 2928)>


** <_DummyThread(Dummy-1, started daemon 1968)> 10


** <_DummyThread(Dummy-1, started daemon 1968)> 11


** <_DummyThread(Dummy-1, started daemon 1968)> 12


** <_DummyThread(Dummy-1, started daemon 1968)> 13


** <_DummyThread(Dummy-1, started daemon 1968)> 14



我实际上希望mythread中断它的执行并运行。 对了,mythread不会中断它的执行,函数,实际上是由主线程运行的。 我试图做反向( 生成的线程向主线程发送一个信号) 。

我想我还没有理解信号是如何工作的,如果有可以能的话。 - 有什么线索- 我在网上找了个解决方案但是没有结果。

谢谢

时间: 原作者:

好的,我可以理解的是,你希望线程在进一步进行迭代之前输入 5第二个睡眠。


import sys


import time


from PyQt4 import QtGui as qt


from PyQt4 import QtCore as qtcore


from PyQt4.QtCore import QThread


import threading



app = qt.QApplication(sys.argv)


class widget(qt.QWidget):



 def __init__(self, parent=None):


 qtcore.QObject.__init__(self)



 def appinit(self):


 self.mysignal = qtcore.SIGNAL("mysignal")


 thread = mythread(self,self.mysignal)


 thread.start()


 time.sleep(5)


 print"before emit",str(threading.current_thread())


 self.emit(self.mysignal,"hello, I'm thread"+str(threading.current_thread()))


 print"after emit",str(threading.current_thread())



class mythread(QThread):


 def __init__(self,parent,sig):


 qtcore.QThread.__init__(self, parent=app)


 self.parent=parent


 self.mysignal = sig


 self.stop_event=threading.Event()


 self.connect(self.parent, self.mysignal, self.setIt)



 def run(self):



 for i in range(15):


 if self.stop_event.isSet():


 self.myfunc()


 print"**",threading.current_thread(),i


 time.sleep(1)



 def setIt(self,msg):


 self.stop_event.set()


 self.msg = msg



 def myfunc(self):


 print threading.current_thread(),self.msg,"Enter"


 time.sleep(5)


 print threading.current_thread(),self.msg,"Exit"


 self.stop_event.clear()



def main():


 mywidget = widget()


 mywidget.show()


 qtcore.QTimer.singleShot(0, mywidget.appinit)


 sys.exit(app.exec_())



main()



输出:


** <_DummyThread(Dummy-1, started daemon 7312)> 0


** <_DummyThread(Dummy-1, started daemon 7312)> 1


** <_DummyThread(Dummy-1, started daemon 7312)> 2


** <_DummyThread(Dummy-1, started daemon 7312)> 3


** <_DummyThread(Dummy-1, started daemon 7312)> 4


before emit <_MainThread(MainThread, started 232)>


after emit <_MainThread(MainThread, started 232)>


<_DummyThread(Dummy-1, started daemon 7312)> hello, I'm thread <_MainThread(MainThread, started 232)> Enter


<_DummyThread(Dummy-1, started daemon 7312)> hello, I'm thread <_MainThread(MainThread, started 232)> Exit


** <_DummyThread(Dummy-1, started daemon 7312)> 5


** <_DummyThread(Dummy-1, started daemon 7312)> 6


** <_DummyThread(Dummy-1, started daemon 7312)> 7


** <_DummyThread(Dummy-1, started daemon 7312)> 8


** <_DummyThread(Dummy-1, started daemon 7312)> 9


** <_DummyThread(Dummy-1, started daemon 7312)> 10


** <_DummyThread(Dummy-1, started daemon 7312)> 11


** <_DummyThread(Dummy-1, started daemon 7312)> 12


** <_DummyThread(Dummy-1, started daemon 7312)> 13


** <_DummyThread(Dummy-1, started daemon 7312)> 14



原作者:
...