Python多处理无法在KeyboardInterrupt上终止,冻结了Windows

时间:2018-01-06 21:53:49

标签: python-3.x python-multiprocessing

我有一个简单的函数getHashes(),它在提供数据列表/字典时计算校验和。我正在使用多处理来并行计算这些哈希值,使用多个核心。但是,如果我按CTRL + C结束进程,我的整个机器都会锁定。我在Windows 10上使用Python 3.6。这是 功能供参考:

def syncHashes():
    """ 
    main function
    """
    #Initialize
    print("Starting the scanner in root " + rootDir)

    #Start the multiprocessing pool
    pool = multiprocessing.Pool(processes=10)

    for dirName, subdirList, fileList in os.walk(rootDir):
        # Convert filenames to full paths...
        full_path_fnames = map(lambda fn: os.path.join(dirName, fn), 
                           fileList)

        #Only add valid files
        full_path_fnames = filter(is_valid_file, full_path_fnames)

        try:
            #Send all files in the directory to the getHashes function
            results = pool.map(getHashes, full_path_fnames)
            pool.close()

            #Necessary to allow CTRL+C interruptions
            for item in results:
                item.wait(timeout=9999999)

        except KeyboardInterrupt:
            pool.terminate()
        finally:
            pool.join()

if __name__ == "__main__":
    syncHashes()

我尝试添加等待以允许工作线程完成,但这仍然不成功。任何帮助将不胜感激。

0 个答案:

没有答案