Skip to main content
 首页 » 编程设计

python多处理模块导致无限循环

2024年12月31日9dflying

我正在尝试使用多处理中的池来加速我的读/写功能。但是,当我尝试使用 pyinstaller 并使用 exe 运行它时。这 结果是一个新进程产生一个新进程产生一个 无限的新进程,直到我注销并强制操作系统 终止我所有的用户进程。我不知道发生了什么

在pycharm中运行甚至用.py运行代码都没有问题。

版本为python 3.6.3。

平台:Windows 7

代码如下:

if __name__ == "__main__": 
    pool = Pool(2) 
    pool.map(readwritevalue, [file for file in gettxtpath()]) 
    pool.close() 
    pool.join() 
    GetFormated()  ##Get the Final output of .csv 
    Getxlsx()  ##Get the Report.xls 
    GetDocx()  ##Get the docx format 
    t1 = time.time() 
    print("Parallel time{t}s".format(t=time.time() - t1)) 

请您说明一下这个问题好吗? 我搜索了谷歌,一些答案是将多处理模块放在“__name__==”__main__”下,但是,我已经这样做了。那么还有什么会导致进程的无限爆炸呢?

非常感谢。

请您参考如下方法:

感谢 MilanVelebit 帮助找出答案。

我在这里发布我的答案。 导入 multiprocessing 和使用 pyinstaller 时只需要添加一行。

from multiprocessing import Pool,freeze_support 
 
if __name__ == "__main__": 
 
    ##Add support for when a program which uses multiprocessing has been frozen to produce a Windows executable. (Has been tested with py2exe, PyInstaller and cx_Freeze.) 
    #One needs to call this function straight after the '__main__' line of the main module. 
 
    freeze_support() 
    t1 = time.time() 
    pool = Pool(2) 
    pool.map(readwritevalue, [file for file in gettxtpath()]) 
    pool.close() 
    pool.join() 
    GetFormated()  ##Get the Final output of Xunjian-Report.csv 
    Getxlsx()  ##Get the Xunjian-Report.xls 
    GetDocx()  ##Get the docx format 
    print("Cost time {t}".format(t=time.time() - t1))