如何确保QApplication及其QThreads全部关闭

时间:2017-07-06 04:50:18

标签: python qt pyqt pyqt4 pyqt5

下面的代码会创建一个QDialog,它会启动一个QThread,它会获得一个很长的计算函数。修改了QDialog的closeEvent()方法以终止已启动的thread

如果thread完成正在处理的任务,如何确保quit()终止?使用terminate()thread方法停止线程有什么区别?是否应在主应用程序窗口关闭之前终止Python?为什么在Mac OS X上,即使在主对话框关闭且线程终止后,import threading import Queue as Queue import datetime global queue queue = Queue.Queue() class Thread(QThread): def __init__(self, queue, parent): QThread.__init__(self, parent) self.queue = queue def run(self): while True: task = queue.get() output = task() queue.task_done() def longToCalculate(): for i in range(30000000): i += i if not i % 100000: print '%s ...still calculating ' % datetime.datetime.now() print 'calculation completed' return i class Dialog(QDialog): def __init__(self, parent=None): super(Dialog, self).__init__(parent) def closeEvent(self, event): # self.thread.quit() self.thread.terminate() event.accept() class Dialog(QDialog): def __init__(self, parent=None): QDialog.__init__(self, parent) self.queue = Queue.Queue() self.thread = Thread(queue=self.queue, parent=self) self.thread.start() queue.put(longToCalculate) if __name__ == '__main__': app = QApplication([]) dialog = Dialog() dialog.show() qApp.exec_() 进程仍然在活动监视器中列出?

enter image description here

 while ($getschools = $schools-            >fetch_assoc()){ 
 $findschools = $dbcon->query("SELECT *     FROM school_".$getstate['texas']." WHERE      students = '".$studentGPA['best']."'"); 
   while ($beststudents = $findschools->fetch_assoc()){
  $jsonfield.= '{"name":"'.$getstudent['firstname'].'"},';
 $i = 0; 
 $countstudents = $getschool->num_rows;                  
if($i+1 < $countstudents){  //if next index is    smaller than the count then add comma else don't and loop will be terminated
    $jsonfield.=','; 
      }
   }
 }

1 个答案:

答案 0 :(得分:1)

以下是不包含队列的示例代码。

import os, sys
import datetime

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

class Thread( QThread ):
    def __init__( self, parent ):

        QThread.__init__( self, parent )

    def run( self ):
        self.longToCalculate()

    def longToCalculate( self ):
        for i in range( 30000000 ):
            i += i

            if ( i % 1000000 == 0 ):
                print( '%s ...still calculating' % QDateTime.currentDateTime().toString() )

        print( 'calculation completed' )
        return i

class Dialog(QDialog):
    def __init__( self, parent = None ):

        QDialog.__init__( self, parent )

        self.thread = Thread( parent = self )
        self.thread.start()

        self.thread.finished.connect( self.threadComplete )

    def threadComplete( self ) :
        QMessageBox.information( self, "Thread complete", "The thread has finished running. This program wil automatically close now." )
        self.close()

    def closeEvent( self, cEvent ) :

        if self.thread.isRunning() :
            QMessageBox.information( self, "Thread running", "The thread is running. You cannot close this program." )
            cEvent.ignore()

        else :
            cEvent.accept()

if __name__ == '__main__':

    app = QApplication( sys.argv )

    dialog = Dialog()
    dialog.show()

    qApp.exec_()