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



我正在尝试使用来自多处理的池来加快我的读/写功能。但是,当我尝试使用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帮助我们找到答案。

我把答案贴在这里。当您导入多处理并使用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))

最新更新