Repository: espnet/espnet
Issue: Using spawn in multiprocessing causes an error

After #1251 is merged, we encountered the following error.
```
# lm_train.py --config conf/lm.yaml --ngpu 1 --backend pytorch --verbose 1 --outdir exp/train_rnnlm_pytorch_lm_word100 --tensorboard-dir tensorboard/train_rnnlm_pytorch_lm_word100 --train-label data/local/wordlm_train/train.txt --valid-label data/local/wordlm_train/valid.txt --test-label data/local/wordlm_train/test.txt --resume --dict data/local/wordlm_train/wordlist_100.txt 
# Started at Sat Oct 12 15:09:23 UTC 2019
#
Traceback (most recent call last):
  File "/content/espnet/egs/an4/asr1/../../../espnet/bin/lm_train.py", line 170, in <module>
    mp.set_start_method('spawn')
  File "/usr/lib/python3.6/multiprocessing/context.py", line 242, in set_start_method
    raise RuntimeError('context has already been set')
RuntimeError: context has already been set
# Accounting: time=1 threads=1
# Ended (code 1) at Sat Oct 12 15:09:24 UTC 2019, elapsed time 1 seconds
```
@kan-bayashi, can you fix it?
@arendu told me that `mp.set_start_method('spawn', force=True)` may solve it.

Changed files:
espnet/bin/lm_train.py, espnet/bin/tts_train.py, espnet/bin/mt_train.py, espnet/bin/asr_train.py

diff --git a/espnet/bin/asr_train.py b/espnet/bin/asr_train.py
index 734de4bba..bd1af48ab 100755
--- a/espnet/bin/asr_train.py
+++ b/espnet/bin/asr_train.py
@@ -359,5 +359,10 @@ def main(cmd_args):
 
 
 if __name__ == '__main__':
-    mp.set_start_method('spawn')
+    # NOTE(kan-bayashi): setting multiple times causes RuntimeError
+    #   See also https://github.com/pytorch/pytorch/issues/3492
+    try:
+        mp.set_start_method('spawn')
+    except RuntimeError:
+        pass
     main(sys.argv[1:])
diff --git a/espnet/bin/lm_train.py b/espnet/bin/lm_train.py
index 426f3f30b..703323a85 100755
--- a/espnet/bin/lm_train.py
+++ b/espnet/bin/lm_train.py
@@ -167,5 +167,10 @@ def main(cmd_args):
 
 
 if __name__ == '__main__':
-    mp.set_start_method('spawn')
+    # NOTE(kan-bayashi): setting multiple times causes RuntimeError
+    #   See also https://github.com/pytorch/pytorch/issues/3492
+    try:
+        mp.set_start_method('spawn')
+    except RuntimeError:
+        pass
     main(sys.argv[1:])
diff --git a/espnet/bin/mt_train.py b/espnet/bin/mt_train.py
index d5eb1ac25..a963b212b 100755
--- a/espnet/bin/mt_train.py
+++ b/espnet/bin/mt_train.py
@@ -283,5 +283,10 @@ def main(cmd_args):
 
 
 if __name__ == '__main__':
-    mp.set_start_method('spawn')
+    # NOTE(kan-bayashi): setting multiple times causes RuntimeError
+    #   See also https://github.com/pytorch/pytorch/issues/3492
+    try:
+        mp.set_start_method('spawn')
+    except RuntimeError:
+        pass
     main(sys.argv[1:])
diff --git a/espnet/bin/tts_train.py b/espnet/bin/tts_train.py
index 3b8743696..6fdd70b8f 100755
--- a/espnet/bin/tts_train.py
+++ b/espnet/bin/tts_train.py
@@ -180,5 +180,10 @@ def main(cmd_args):
 
 
 if __name__ == "__main__":
-    mp.set_start_method('spawn')
+    # NOTE(kan-bayashi): setting multiple times causes RuntimeError
+    #   See also https://github.com/pytorch/pytorch/issues/3492
+    try:
+        mp.set_start_method('spawn')
+    except RuntimeError:
+        pass
     main(sys.argv[1:])