Enabling --onedir
multiprocessing support on Windows (does NOT work with --onefile
on Windows, but otherwise safe on all platforms/configurations):
if __name__ == '__main__':
# On Windows calling this function is necessary.
multiprocessing.freeze_support()
Enabling --onefile
multiprocessing support on Windows (safe on all platforms/configurations, compatible with --onedir
):
import multiprocessing.forking
import os
import sys
class _Popen(multiprocessing.forking.Popen):
def __init__(self, *args, **kw):
if hasattr(sys, 'frozen'):
# We have to set original _MEIPASS2 value from sys._MEIPASS
# to get --onefile mode working.
os.putenv('_MEIPASS2', sys._MEIPASS)
try:
super(_Popen, self).__init__(*args, **kw)
finally:
if hasattr(sys, 'frozen'):
# On some platforms (e.g. AIX) 'os.unsetenv()' is not
# available. In those cases we cannot delete the variable
# but only set it to the empty string. The bootloader
# can handle this case.
if hasattr(os, 'unsetenv'):
os.unsetenv('_MEIPASS2')
else:
os.putenv('_MEIPASS2', '')
class Process(multiprocessing.Process):
_Popen = _Popen
# ...
if __name__ == '__main__':
# On Windows calling this function is necessary.
multiprocessing.freeze_support()
# Use your new Process class instead of multiprocessing.Process
References
https://stackoverflow.com/questions/24944558/pyinstaller-built-windows-exe-fails-with-multiprocessing