解决 RuntimeError: An attempt has been made to start a new process before the current process......

项目场景:

在windows平台下运行python代码报错:

RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

原因分析:

①这是因为在linux系统中可以使用多个子进程加载数据,而在windows系统中不能。
②这个错误是由于在 Windows 平台上,使用多进程时需要在代码中添加if __name__ == '__main__':条件判断,以确保主模块在启动子进程之前已经完成了初始化阶段。


解决方案:

1、如果代码中有使用了num_worker, 在windows系统中需要将进程数设置为单进程,所以我们需要将数据处理语句这部分的参数改为num_workers=0即可解决错误
解决 RuntimeError: An attempt has been made to start a new process before the current process......_第1张图片

2、我的代码是第二种情况,要解决这个问题,你可以将你的代码包装在 if __name__ == '__main__': 条件判断下,就可解决。

解决 RuntimeError: An attempt has been made to start a new process before the current process......_第2张图片

你可能感兴趣的:(pytorch,python,pytorch)