Is A PyTorch Dataset Accessed by Multiple DataLoader Workers?
When using more than 1 DataLoader workers in PyTorch, does every worker access the same Dataset instance? Or does each DataLoader worker have their own instance of Dataset?

from torch.utils.data import DataLoader, Dataset

class NumbersDataset(Dataset):
    def __init__(self):
        self.samples = list(range(1, 1001))

    def __len__(self):
        return len(self.samples)

    def __getitem__(self, idx):
        return self.samples[idx]

dataset = NumbersDataset()
train_loader = DataLoader(dataset, num_workers=4)



It seams like they are accessing to the same instance. I have tried adding a static variable inside the dataset class and incrementing it every time a new instance is created. Code can be found below.

from torch.utils.data import DataLoader, Dataset


class NumbersDataset(Dataset):
    i = 0

    def __init__(self):
        NumbersDataset.i += 1
        self.samples = list(range(1, 1001))

    def __len__(self):
        return len(self.samples)

    def __getitem__(self, idx):
        return self.samples[idx]


dataset_1 = NumbersDataset()
train_loader = DataLoader(dataset_1, num_workers=4)

for i, data in enumerate(train_loader):
    pass

dataset_2 = NumbersDataset()
train_loader = DataLoader(dataset_2, num_workers=4)

for i, data in enumerate(train_loader):
    pass

print(NumbersDataset.i)


The output is 2. Hope it helps :D
