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_())
Run the above code using windows command prompt or power shell
pyinstaller.exe gui.py
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With