How to make `num_workers > 0` work on Windows10?

username: feiyuhuahuo
cooked: <p>My PyTorch version is 1.8.1.<br>
I have a piece of code below,</p>
<pre><code class="lang-auto">import torch.utils.data as data
import numpy as np
from resnet import ResNet

class CLS:
    def __init__(self):
        self.model_k = 'res50'
        self.model = ResNet(depth=50, num_classes=7)

class CLS_dataset(data.Dataset):
    def __init__(self, cfg):
        self.cfg = cfg

    def __getitem__(self, index):
        img = np.random.randn(3, 512, 512).astype('float32')
        label = 0
        return img, label

    def __len__(self):
        return 100

if __name__ == '__main__':
    cfg = CLS()
    net = cfg.model.cuda().train()
    dataset = CLS_dataset(cfg)

    data_loader = data.DataLoader(dataset, batch_size=4, num_workers=4, shuffle=True, pin_memory=True)

    for images, targets in data_loader:
        images = images.cuda().detach()
        targets = targets.cuda().detach()
        print(targets)

        loss = net(images, targets)
</code></pre>
<p>Directly run the code in Anaconda Prompt just get the following error:</p>
<pre><code class="lang-auto">RuntimeError: cuda runtime error (801) : operation not supported at ..\torch/csrc/generic/StorageSharing.cpp:253

(installer) E:\classification_issue&gt;Traceback (most recent call last):
  File "&lt;string&gt;", line 1, in &lt;module&gt;
  File "C:\ProgramData\Anaconda3\envs\installer\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\ProgramData\Anaconda3\envs\installer\lib\multiprocessing\spawn.py", line 126, in _main
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
</code></pre>
<p>I also try tried make the <code>if __name__=='__main__'</code> just above the <code>class CLS_dataset(data.Dataset)</code> or  just above the <code>class CLS</code>. None of them worked.</p>
<p>But if I do the following comment, the code can run successfully.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/a/c/ac157a460e656fe0a516fbfd5a394ebe1ddc517a.png" alt="image" data-base62-sha1="oyk8Icl6l5LoloUM5izOm4cXDB0" width="410" height="145"><br>
Or I just create the <code>net</code> with <code>Resnet()</code> directly,  the code can also run successfully.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/b/2ba7b10ae77216fad62a387e2cdf8bfc11f2f407.png" data-download-href="https://discuss.pytorch.org/uploads/default/2ba7b10ae77216fad62a387e2cdf8bfc11f2f407" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/b/2ba7b10ae77216fad62a387e2cdf8bfc11f2f407.png" alt="image" data-base62-sha1="6ebO8TKAmt1wNHKfUEWo1blyTJB" width="689" height="500" data-dominant-color="2F3131"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">747×542 37.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>What’s the reason behind it?</p>