RuntimeError already started
Env:
- os: Ubuntu
- python3
- pytorch
- vscode
Desc
- 在上述环境中运行A3C多进程模型,使用命令行时没问题,使用vscode时出现 'RuntimeError: already started' 的错误, 具体错误信息如下:
E00000.755: Exception escaped from start_client
Traceback (most recent call last):
File "/home/haikuoxin/.vscode/extensions/ms-python.python-2019.11.50794/pythonFiles/lib/python/old_ptvsd/ptvsd/log.py", line 110, in g
return f(*args, **kwargs)
File "/home/haikuoxin/.vscode/extensions/ms-python.python-2019.11.50794/pythonFiles/lib/python/old_ptvsd/ptvsd/pydevd_hooks.py", line 74, in start_client
sock, start_session = daemon.start_client((host, port))
File "/home/haikuoxin/.vscode/extensions/ms-python.python-2019.11.50794/pythonFiles/lib/python/old_ptvsd/ptvsd/daemon.py", line 214, in start_client
with self.started():
File "/home/haikuoxin/anaconda3/lib/python3.6/contextlib.py", line 81, in __enter__
return next(self.gen)
File "/home/haikuoxin/.vscode/extensions/ms-python.python-2019.11.50794/pythonFiles/lib/python/old_ptvsd/ptvsd/daemon.py", line 110, in started
self.start()
File "/home/haikuoxin/.vscode/extensions/ms-python.python-2019.11.50794/pythonFiles/lib/python/old_ptvsd/ptvsd/daemon.py", line 145, in start
raise RuntimeError('already started')
RuntimeError: already started
Traceback (most recent call last):
Error in atexit._run_exitfuncs:
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
Solution
在自己的代码头添加以下代码行:
import torch.multiprocessing as mp
mp.set_start_method('spawn', True)