RuntimeError: context has already been set

RuntimeError: context has already been set解决方案

问题出现的原因:
当 multiprocessing 模块被第二次配置 start_method 时会报错

import multiprocessing

if __name__ == '__main__':
    multiprocessing.set_start_method('spawn')
    multiprocessing.set_start_method('spawn')

或者在 get_start_method 后配置 start_method 也会报错:

import multiprocessing

if __name__ == '__main__':
    print(multiprocessing.get_start_method())
    multiprocessing.set_start_method('spawn')

错误信息:

RuntimeError: context has already been set

在这里插入图片描述

解决方案:
在配置 start_method 时配置参数 force=True

import multiprocessing

if __name__ == '__main__':
    print(multiprocessing.get_start_method())
    multiprocessing.set_start_method('spawn', force=True)

你可能感兴趣的:(遇到的bug,python,torch,多线程)