python multiprocessing模块导致无限循环

时间:2018-07-20 11:46:07

标签: python multiprocessing

我正在尝试使用多处理中的缓冲池来加快我的读/写功能。但是,当我尝试使用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))

请您对此进行说明? 我在Google上搜索过,一些答案是将多处理模块放在“ __name__=="__main__”下,但是,我已经这样做了,那么还有什么会导致进程的无限爆炸?

非常感谢您。

1 个答案:

答案 0 :(得分:1)

感谢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))
相关问题