Custom dataset throws error when using multiple workers

I tried implementing a custom dataset, which works fine, but as soon as I specify to use multiple workers for accelerating the loading, I get a huge chained error message. The example here doesn’t make any practical sense, it is just the minimal example to reproduce the error. Curiously, the error happens on windows, but not when running it on ubuntu. (both pytorch 1.6, and I tested both python 3.7 and 3.8 on both systems)

Can anyone give me a hint what is going wrong and possibly how to fix it?

import torch
class MyDataset(
    def __init__(self):

    def __len__(self):
        return 10

    def __getitem__(self, idx):
        return torch.ones((1,))

dataset1 = MyDataset()
loader =
    dataset1, batch_size=2, num_workers=0)
x = next(iter(loader))  # works fine

dataset2 = MyDataset()
loader_parallel =
    dataset2, batch_size=2, num_workers=1)
y = next(iter(loader_parallel))  # explodes

The error message is

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\multiprocessing\", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\multiprocessing\", line 114, in _main
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\multiprocessing\", line 225, in prepare
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\multiprocessing\", line 277, in _fixup_main_from_path
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\", line 85, in _run_code
    exec(code, run_globals)
  File "path\to\my\", line 27, in <module>
    y = next(iter(loader_parallel))  # explodes
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\site-packages\torch\utils\data\", line 279, in __iter__
    return _MultiProcessingDataLoaderIter(self)
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\site-packages\torch\utils\data\", line 719, in __init__
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\multiprocessing\", line 112, in start
    self._popen = self._Popen(self)
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\multiprocessing\", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\multiprocessing\", line 322, in _Popen
    return Popen(process_obj)
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\multiprocessing\", line 46, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\multiprocessing\", line 143, in get_preparation_data
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\multiprocessing\", line 136, in _check_not_importing_main
    is not going to be frozen to produce an executable.'')
        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__':

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\site-packages\torch\utils\data\", line 761, in _try_get_data
    data = self._data_queue.get(timeout=timeout)
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\multiprocessing\", line 105, in get
    raise Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "path\to\my\", line 27, in <module>
    y = next(iter(loader_parallel))  # explodes
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\site-packages\torch\utils\data\", line 345, in __next__
    data = self._next_data()
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\site-packages\torch\utils\data\", line 841, in _next_data
    idx, data = self._get_data()
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\site-packages\torch\utils\data\", line 808, in _get_data
    success, data = self._try_get_data()
  File "C:\ProgramData\Anaconda3\envs\smp2\lib\site-packages\torch\utils\data\", line 774, in _try_get_data
    raise RuntimeError('DataLoader worker (pid(s) {}) exited unexpectedly'.format(pids_str))
RuntimeError: DataLoader worker (pid(s) 5144) exited unexpectedly

Could you add the if-clause protection as described in the Windows FAQ?


I didn’t realize some things were OS-specific, this solved my issue, thanks a lot!