Runtime error on multithread on windows

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)

When i run i have this error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  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 <module>
    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.

The project is a training of different models using cuda.
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.

1 Like

Did you add the suggested if-clause protection?

No, I don’t know what it means… :sweat_smile:

Ha, I thought you might have overlooked the suggested fix. :wink:

Have a look at the Windows FAQ, where the if-clause protection is given as a code snippet:

import torch

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

if __name__ == '__main__':
    main()
1 Like

Yes i used the freeze support (torch.multiprocessing.freeze_support()) and it works.
Do you think that i loss speed in training?

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

No, but i 'll check it surely.
Thank you for the help and happy new year :slight_smile: