在windows上调用function by multiprocessing模块时python可执行文件为什么会打开新的窗口实例

2024-04-26 12:03:41 发布

您现在位置:Python中文网/ 问答频道 /正文

简短问题:在windows操作系统上调用多处理模块的函数时,为什么pyinstaller生成的python可执行文件会打开新的窗口实例

我有一个用pyside编写的GUI代码。当我们点击简单按钮时,它将计算另一个进程中的阶乘(使用多处理模块)。 当我运行python程序时,它可以正常工作。但在我使用PyInstaller创建可执行文件之后,当我使用exe运行时,它是在多处理模块调用函数时创建的新窗口。下面是重现问题的代码和逐步过程。在

代码:

import sys
import multiprocessing

from PySide import QtGui
from PySide import QtCore


def factorial():
        f = 4
        r = 1
        for i in reversed(range(1, f+1)):
            r *= i 
        print 'factorial', r


class MainGui(QtGui.QWidget):
    def __init__(self):
        super(MainGui, self).__init__()

        self.initGui()

    def initGui(self):
        b = QtGui.QPushButton('click', self)
        b.move(30, 30)
        b.clicked.connect(self.onClick)
        self.resize(600, 400)
        self.show()

    def onClick(self):
        print 'button clicked'
        self.forkProcess()

    def forkProcess(self):
        p = multiprocessing.Process(target=factorial)
        p.daemon = True
        p.start()



if __name__ == "__main__":
    print 'ok'

    app = QtGui.QApplication(sys.argv)
    ex = MainGui()
    sys.exit(app.exec_())
  1. 使用windows命令提示符或powershell运行上述代码

    在pyinstaller.exe图形用户界面.py

  2. 打开dist/gui/图形用户界面.exe(距离\gui\图形用户界面.exe). 您将打开一个窗口

enter image description here

当我们点击按钮点击时,它会计算阶乘,但会创建一个新的窗口实例。很奇怪。当我在创建可执行文件之前执行程序,或者在linux上,它不会发生。它只在我执行生成的python可执行文件时发生

单击单击按钮后的屏幕截图

enter image description here


Tags: 模块代码importself可执行文件windowsdefsys