Pyinstaller 多进程代码打包 exe 出现多个进程解决方案

Pyinstaller 多进程代码打包 exe 出现多个进程解决方案

在执行项目的根目录添加 multiprocessing_win.py

import os  
import sys  
import multiprocessing  
  

try:  
    
    if sys.platform.startswith('win'):  
        import multiprocessing.popen_spawn_win32 as forking  
    else:  
        import multiprocessing.popen_fork as forking  
except ImportError:  
    import multiprocessing.forking as forking  
  
if sys.platform.startswith('win'):  
    
    class _Popen(forking.Popen):  
        def __init__(self, *args, **kw):  
            if hasattr(sys, 'frozen'):  
                
                
                os.putenv('_MEIPASS2', sys._MEIPASS)  
            try:  
                super(_Popen, self).__init__(*args, **kw)  
            finally:  
                if hasattr(sys, 'frozen'):  
                    
                    
                    
                    
                    if hasattr(os, 'unsetenv'):  
                        os.unsetenv('_MEIPASS2')  
                    else:  
                        os.putenv('_MEIPASS2', '')  
  
    
    forking.Popen = _Popen   ​

在执行多进程代码 py 文件中添加

import multiprocessing_win
import multiprocessing

然后在 if __name__ == '__main__':的方法下面添加

multiprocessing.freeze_support()

实例如下:

import multiprocessing_win
import multiprocessing

if __name__ == '__main__':
    multiprocessing.freeze_support()
    app.run() 
    

参考文档
https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessing

你可能感兴趣的:(python)