<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>

<p dir="auto">BrokenPipeError on Windows when setting the num_workers as 4 in DataLoader but 2 num_workers is fine.</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<ol dir="auto">
<li></li>
<li></li>
<li></li>
</ol>

<p dir="auto">I have submitted this issue in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="540202633" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/31465" data-hovercard-type="issue" data-hovercard-url="/pytorch/pytorch/issues/31465/hovercard" href="https://github.com/pytorch/pytorch/issues/31465">#31465</a>, and the contributor commented<a href="https://github.com/pytorch/pytorch/issues/31465#issuecomment-567435296" data-hovercard-type="issue" data-hovercard-url="/pytorch/pytorch/issues/31465/hovercard"></a>  as</p>
<blockquote>
<p dir="auto">See <a href="https://pytorch.org/docs/stable/notes/windows.html#multiprocessing-error-without-if-clause-protection" rel="nofollow">https://pytorch.org/docs/stable/notes/windows.html#multiprocessing-error-without-if-clause-protection</a>.</p>
</blockquote>
<p dir="auto">I opened the link and found the relevant content <a href="https://pytorch.org/docs/stable/notes/windows.html#multiprocessing-error-broken-pipe" rel="nofollow"><em>Multiprocessing error “Broken pipe”</em> </a>  as</p>
<blockquote>
<p dir="auto">Multiprocessing error “Broken pipe”<br>
ForkingPickler(file, protocol).dump(obj)<br>
BrokenPipeError: [Errno 32] Broken pipe<br>
This issue happens when the child process ends before the parent process finishes sending data. There may be something wrong with your code. You can debug your code by reducing the num_worker of DataLoader to zero and see if the issue persists.</p>
</blockquote>
<p dir="auto">But my code ran normally if I set the num_workers as 2.<br>
However, the 4 num_workers would still cause the BrokenPipeError.</p>
<h2 dir="auto">Expected behavior</h2>

<p dir="auto">I want to set the num_workers as 4 instead of 2.</p>
<h2 dir="auto">Environment</h2>
<p dir="auto">Please copy and paste the output from our<br>
<a href="https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py" rel="nofollow">environment collection script</a><br>
(or fill out the checklist below manually).</p>
<p dir="auto">You can get the script and run it with:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py"><pre class="notranslate"><code class="notranslate">wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py
</code></pre></div>
<ul dir="auto">
<li>PyTorch Version (e.g., 1.0): 1.3.1</li>
<li>OS (e.g., Linux): Win10</li>
<li>How you installed PyTorch (<code class="notranslate">conda</code>, <code class="notranslate">pip</code>, source): conda</li>
<li>Build command you used (if compiling from source): by PyCharm</li>
<li>Python version: 3.7.3</li>
<li>CUDA/cuDNN version: CUDA 10.1</li>
<li>GPU models and configuration: using CPU</li>
<li>Any other relevant information:</li>
</ul>
<h2 dir="auto">Additional context</h2>
<h2 dir="auto">🐛 Bug</h2>

<p dir="auto">BrokenPipeError on Windows when setting the num_workers as 4 in DataLoader</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<ol dir="auto">
<li>I used code like this,</li>
</ol>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content=" testloader = torch.utils.data.DataLoader(testset, batch_size=4,
                                             shuffle=False, num_workers=4)"><pre class="notranslate"> <span class="pl-s1">testloader</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-s1">utils</span>.<span class="pl-s1">data</span>.<span class="pl-v">DataLoader</span>(<span class="pl-s1">testset</span>, <span class="pl-s1">batch_size</span><span class="pl-c1">=</span><span class="pl-c1">4</span>,
                                             <span class="pl-s1">shuffle</span><span class="pl-c1">=</span><span class="pl-c1">False</span>, <span class="pl-s1">num_workers</span><span class="pl-c1">=</span><span class="pl-c1">4</span>)</pre></div>
<ol start="2" dir="auto">
<li></li>
<li></li>
</ol>

<blockquote>
<p dir="auto">Here are the error messages:<br>
Traceback (most recent call last):<br>
File "", line 1, in <br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\multiprocessing\spawn.py", line 105, in spawn_main<br>
exitcode = _main(fd)<br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\multiprocessing\spawn.py", line 114, in _main<br>
prepare(preparation_data)<br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\multiprocessing\spawn.py", line 225, in prepare<br>
_fixup_main_from_path(data['init_main_from_path'])<br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path<br>
run_name="<strong>mp_main</strong>")<br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\runpy.py", line 263, in run_path<br>
pkg_name=pkg_name, script_name=fname)<br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\runpy.py", line 96, in _run_module_code<br>
mod_name, mod_spec, pkg_name, script_name)<br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\runpy.py", line 85, in <em>run_code<br>
exec(code, run_globals)<br>
File "D:\Programs\Kaggle\Digit Recognizer\digit-recognizer\main_predict.py", line 7, in <br>
import torch<br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\site-packages\torch_<em>init</em></em>.py", line 81, in <br>
from torch._C import *<br>
ImportError: DLL load failed: 页面文件太小，无法完成操作。<br>
Traceback (most recent call last):<br>
File "D:/Programs/Kaggle/Digit Recognizer/digit-recognizer/main_predict.py", line 62, in <br>
main()<br>
File "D:/Programs/Kaggle/Digit Recognizer/digit-recognizer/main_predict.py", line 47, in main<br>
for data in testloader:<br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\site-packages\torch\utils\data\dataloader.py", line 278, in <strong>iter</strong><br>
return _MultiProcessingDataLoaderIter(self)<br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\site-packages\torch\utils\data\dataloader.py", line 682, in <strong>init</strong><br>
w.start()<br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\multiprocessing\process.py", line 112, in start<br>
self._popen = self._Popen(self)<br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\multiprocessing\context.py", line 223, in _Popen<br>
return _default_context.get_context().Process._Popen(process_obj)<br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\multiprocessing\context.py", line 322, in _Popen<br>
return Popen(process_obj)<br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\multiprocessing\popen_spawn_win32.py", line 89, in <strong>init</strong><br>
reduction.dump(process_obj, to_child)<br>
File "E:\Path\Python\Anaconda3\envs\env_general\lib\multiprocessing\reduction.py", line 60, in dump<br>
ForkingPickler(file, protocol).dump(obj)<br>
BrokenPipeError: [Errno 32] Broken pipe<br>
Process finished with exit code 1</p>
</blockquote>
<p dir="auto">And here is the code sample:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="source = &quot;../data/digit-recognizer/test.csv&quot;
    transform = transforms.Compose(
        [transforms.ToTensor(),
         transforms.Normalize((0.5,), (0.5,))])
    # 其中transforms.ToTensor()的作用为将维度顺序为H*W*C的图像张量装换为适合于PyTorch的C*H*W的张量
    testset = MNISTTestDataset(csv_file=source,
                               transform=transform)
    testloader = torch.utils.data.DataLoader(testset, batch_size=4,
                                             shuffle=False, num_workers=4)
    # 如果有问题的话，就把num_workers设成0

    recognizer = DigitRecognizer()
    index = 0

    for data in testloader:
        index = index + 1
        if index &gt; 6:
            break"><pre class="notranslate"><span class="pl-s1">source</span> <span class="pl-c1">=</span> <span class="pl-s">"../data/digit-recognizer/test.csv"</span>
    <span class="pl-s1">transform</span> <span class="pl-c1">=</span> <span class="pl-s1">transforms</span>.<span class="pl-v">Compose</span>(
        [<span class="pl-s1">transforms</span>.<span class="pl-v">ToTensor</span>(),
         <span class="pl-s1">transforms</span>.<span class="pl-v">Normalize</span>((<span class="pl-c1">0.5</span>,), (<span class="pl-c1">0.5</span>,))])
    <span class="pl-c"># 其中transforms.ToTensor()的作用为将维度顺序为H*W*C的图像张量装换为适合于PyTorch的C*H*W的张量</span>
    <span class="pl-s1">testset</span> <span class="pl-c1">=</span> <span class="pl-v">MNISTTestDataset</span>(<span class="pl-s1">csv_file</span><span class="pl-c1">=</span><span class="pl-s1">source</span>,
                               <span class="pl-s1">transform</span><span class="pl-c1">=</span><span class="pl-s1">transform</span>)
    <span class="pl-s1">testloader</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-s1">utils</span>.<span class="pl-s1">data</span>.<span class="pl-v">DataLoader</span>(<span class="pl-s1">testset</span>, <span class="pl-s1">batch_size</span><span class="pl-c1">=</span><span class="pl-c1">4</span>,
                                             <span class="pl-s1">shuffle</span><span class="pl-c1">=</span><span class="pl-c1">False</span>, <span class="pl-s1">num_workers</span><span class="pl-c1">=</span><span class="pl-c1">4</span>)
    <span class="pl-c"># 如果有问题的话，就把num_workers设成0</span>

    <span class="pl-s1">recognizer</span> <span class="pl-c1">=</span> <span class="pl-v">DigitRecognizer</span>()
    <span class="pl-s1">index</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span>

    <span class="pl-k">for</span> <span class="pl-s1">data</span> <span class="pl-c1">in</span> <span class="pl-s1">testloader</span>:
        <span class="pl-s1">index</span> <span class="pl-c1">=</span> <span class="pl-s1">index</span> <span class="pl-c1">+</span> <span class="pl-c1">1</span>
        <span class="pl-k">if</span> <span class="pl-s1">index</span> <span class="pl-c1">&gt;</span> <span class="pl-c1">6</span>:
            <span class="pl-k">break</span></pre></div>
<h2 dir="auto">Expected behavior</h2>

<p dir="auto">I want setting the num_workers as 4 in DataLoader.</p>
<h2 dir="auto">Environment</h2>
<p dir="auto">Please copy and paste the output from our<br>
<a href="https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py" rel="nofollow">environment collection script</a><br>
(or fill out the checklist below manually).</p>
<p dir="auto">You can get the script and run it with:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py"><pre class="notranslate"><code class="notranslate">wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py
</code></pre></div>
<ul dir="auto">
<li>PyTorch Version (e.g., 1.0): 1.3.1</li>
<li>OS (e.g., Linux): Win10</li>
<li>How you installed PyTorch (<code class="notranslate">conda</code>, <code class="notranslate">pip</code>, source): conda</li>
<li>Build command you used (if compiling from source): by PyCharm</li>
<li>Python version: 3.7.3</li>
<li>CUDA/cuDNN version: CUDA 10.1</li>
<li>GPU models and configuration: using CPU</li>
<li>Any other relevant information:</li>
</ul>
<h2 dir="auto">Additional context</h2>

<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ssnl/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ssnl">@ssnl</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/peterjc123/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/peterjc123">@peterjc123</a></p>