Runtime error on multithread on windows

Giuseppe: <p>Hy all, when i run  project in linux it works, when i run in windows it doesn’t work. (now i am unable to use linux at the moment)</p>
<p>When i run i have this error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "&lt;string&gt;", line 1, in &lt;module&gt;
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\multiprocessing\spawn.py", line 125, in _main
    prepare(preparation_data)
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\multiprocessing\spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\runpy.py", line 265, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\GIUSEPPEPUGLISI\Desktop\punto 2 pallini multipli\main.py", line 16, in &lt;module&gt;
    train.start_all()
  File "C:\Users\GIUSEPPEPUGLISI\Desktop\punto 2 pallini multipli\train.py", line 120, in start_all
    model_trained= train(model, train_loader, valid_loader, exp_name="AutoencoderMaxDots", epochs=500)
  File "C:\Users\GIUSEPPEPUGLISI\Desktop\punto 2 pallini multipli\train.py", line 51, in train
    for i, batch in enumerate(loader[mode]):
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 352, in __iter__
    return self._get_iterator()
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 294, in _get_iterator
    return _MultiProcessingDataLoaderIter(self)
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 801, in __init__
    w.start()
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "C:\Users\GIUSEPPEPUGLISI\anaconda3\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
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.
</code></pre>
<p>The project is a training of different models using cuda.<br>
How can i solve this issue? I think that the multithreading is different in windows but i can’t know ho to solve programmatically with python.</p>

ptrblck: <p>Did you add the suggested if-clause protection?</p>

Giuseppe: <p>No, I don’t know what it means… <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=9" title=":sweat_smile:" class="emoji" alt=":sweat_smile:"></p>

ptrblck: <p>Ha, I thought you might have overlooked the suggested fix. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
<p>Have a look at the <a href="https://pytorch.org/docs/stable/notes/windows.html#usage-multiprocessing">Windows FAQ</a>, where the if-clause protection is given as a code snippet:</p>
<pre><code class="lang-python">import torch

def main()
    for i, data in enumerate(dataloader):
        # do something here

if __name__ == '__main__':
    main()
</code></pre>

Giuseppe: <p>Yes i used the freeze support (torch.multiprocessing.freeze_support()) and it works.<br>
Do you think that i loss speed in training?</p>

ptrblck: <p>I’m not sure if you need to use <code>freeze_support()</code>. Have you checked, if your code is executable with the if-clause protection alone?</p>

Giuseppe: <p>No,  but i 'll check it surely.<br>
Thank you for the help and happy new year <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>