# Errors when using num\_workers\>0 in DataLoader

**URL:** <https://discuss.pytorch.org/t/errors-when-using-num-workers-0-in-dataloader/97564>\
**Category:** Uncategorized\
**Created:** [September 26, 2020, 11:05am UTC](https://discuss.pytorch.org/t/errors-when-using-num-workers-0-in-dataloader/97564 "2020-09-26T11:05:44Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![simoloca](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@simoloca](https://discuss.pytorch.org/u/simoloca)\
**Post date:** [September 26, 2020, 4:08pm UTC](https://discuss.pytorch.org/t/errors-when-using-num-workers-0-in-dataloader/97564/4 "2020-09-26T16:08:52Z")

</div>

Ok, it seems that I have solved the problem, and now I can set the num\_workers\>0.  
What I did was just to insert all the code that train the network in a function: train(), summaring:

```auto
def train():
    # Here was inserted the whole code that train the network ...
if __name__ == ' __main__':
    train()

```

Could anyone explain how and why this works?

---

_[View the full topic](https://discuss.pytorch.org/t/errors-when-using-num-workers-0-in-dataloader/97564)._
