RuntimeError: An attempt has been made to start a new process before the current process has finishe

在运行代码的时候出现了这么一个错误:
RuntimeError: An attempt has been made to start a new process before the current process has finishe_第1张图片在多方查找之后发现报错原因是代码中使用了pool = multiprocessing.Pool(os.cpu_count()),在linux系统中可以使用多个子进程加载数据,而在windows系统中不能,我的代码是在Windows系统下跑的,所以会报错。
解决方案:
网上的解决方案基本上都是加main函数或者把num_workers改为0:
1、把代码的主体放到if name=='main’中,继续多进程加载
2、将num_workers设置为0或者采用默认为0的设置default=0

第一种方法亲测不会报错,但是输出的结果我没太看懂
在这里插入图片描述
第二种方法我不知道这个num_workers在哪里,所以没有测试第二种方法。

我自己是采用了把并行改成循环的方式,用循环按顺序挨个跑线程。因为数据量不算大,所以用时还可以,不算长。输出的结果也比较正常。
RuntimeError: An attempt has been made to start a new process before the current process has finishe_第2张图片

你可能感兴趣的:(python,机器学习)