Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

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

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 (distguigui.exe). You will have one window opens

enter image description here

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

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...