使用pyinstaller打包python程序时,打包的exe程序运行时出现进程不断增加至占满cpu使电脑死机
运行环境:
windows 10
python 3.7.7
pyinstaller 3.6
解决方法:
添加一条语句(pyinstaller >= 3.3)
if __name__=='__main__:' multiprocessing.freeze_support() # 添加此条 # 以下为程序代码 main()
pyinstaller < 3.3 参照 官方在github的解释
新建文件 multiprocess.py
内容如下:
import osimport sys# Module multiprocessing is organized differently in Python 3.4+try: # Python 3.4+ if sys.platform.startswith('win'): import multiprocessing.popen_spawn_win32 as forking else: import multiprocessing.popen_fork as forkingexcept ImportError: import multiprocessing.forking as forkingif sys.platform.startswith('win'): # First define a modified version of Popen. class _Popen(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', '') # Second override 'Popen' class with our modified version. forking.Popen = _Popen
在程序中引入模块即可
from multiprocess import *