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

参考:
https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessing
http://blog.csdn.net/xiong_big/article/details/54614231

在工程下新建py文件 frozen.py

import os  
import sys  
import multiprocessing  
  
# 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 forking  
except ImportError:  
    import multiprocessing.forking as forking  
  
if 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   ​

在多进程代码py文件中加入

import frozen 

最后直接在 if name=='main': 的下面一行加入:

multiprocessing.freeze_support()
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容