Qt与multithreading.Process模块使用中遇到的问题

在改写cookiemonster时遇到了此问题,暂时通过将browser模块抽出来成为单独的py然后传参数execl解决,但仍不清楚为何Process中QApplication退出后开启新的event loop不可行

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from multiprocessing import Queue, Pipe, Process
import time
def test():
        print QApplication.instance()
        QApplication.quit()
        app = QApplication(sys.argv)
        win = QMainWindow()
        win.show()
        time.sleep(4)
        print app
        app.exec_()
def gao():
        p = Process(target=test,args=())
        p.start()
if __name__ == "__main__":
        app = QApplication(sys.argv)
        print app
        win = QMainWindow()
        win.show()
        QTimer.singleShot(2000,gao)
        app.exec_()
        print "quit"

这个运行的结果期待的是出现两个窗口,但实际上只出现一个窗口,输出

<PyQt4.QtGui.QApplication object at 0xac44a2c>
<PyQt4.QtGui.QApplication object at 0xac44a2c>
<PyQt4.QtGui.QApplication object at 0xac44bec>
QCoreApplication::exec: The event loop is already running

理论来说QApplication调用quit之后会结束当前运行的event loop,但实际上在这个例子中没有退出。而且父进程的event loop为何会影响子进程?还不明白,尚待研究

Leave a Reply

Your email address will not be published. Required fields are marked *