Why python executable opens new window instance when function by multiprocessing module is called on windows

旧时模样 提交于 2019-11-28 08:57:13

问题


Short Question: Why python executable generated by pyinstaller opens new window instance when function by multiprocessing module is called on windows operating system

I have a GUI code written using pyside. Where when we click on simple button it will calculate factorial in another process (using multiprocessing module). It works as expected when I run python program. But after I create executable using PyInstaller and when I run using exe it is creating new window when function by multiprocessing module gets called. Here is the code and step by step process to reproduce the issue.

Code:

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. Run the above code using windows command prompt or power shell

    pyinstaller.exe gui.py

  2. Open the dist/gui/gui.exe (dist\gui\gui.exe). You will have one window opens

When we click on button click it's calculating factorial but create a new window instance. It's weird. It's not happening when I execute program before I create executable or on linux. It's only happening when I execute generated python executable file

The screen shot after I click click button


回答1:


If you want to use multiprocessing as a frozen executable, you need to call multiprocessing.freeze_support() at the beginning of your main script. This will allow multiprocessing to "take over" when it spawns its worker processes.

See also https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessing



来源:https://stackoverflow.com/questions/33970690/why-python-executable-opens-new-window-instance-when-function-by-multiprocessing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!