Do multiple Dataloader processes share resource reader instances?

mattrobin: <p>I’m running into a problem with my data file reader, and I believe it may be because this specific file reader can’t be used by multiple processes at the same time. Does the Dataloader’s processes share the file readers? That is, if my custom Dataset has a <code>self.my_file_reader = MyFileReader(file_path)</code>, which is then used during a <code>__getitem__</code>, is the same reader being used by all of the Dataloader processes? Or does each process open it’s own instance of <code>MyFileReader</code>? If they all are sharing the one instance, is there any easy way to change that so that they each create their own instance? Thank you!</p>

SimonW: <p>Different processes do not share memory. So after the loader is running, they are different objects.</p>

mattrobin: <p>Thank you! Now, I just need to figure out what else is causing the problem…</p>

SimonW: <p>What is the issue specifically?</p>

mattrobin: <p>I’m using <code>imageio</code>'s video reader to read frames from a video. I’m getting an error (seemingly consistently between 10,000 to 20,000 steps of training, never mind specifically how much that is) that <code>imageio.core.format.CannotReadFrameError: Could not read frame -1: Frame is 0 bytes, but expected 1244160</code>. This is not due to me passing in the wrong index (this causes different errors when I purposely cause this to happen). It seems to be internal to <code>imageio</code>, and I thought it might be due to the multiprocessing in someway (it still <em>possibly</em> might be, but not due to a shared reader).</p>
<p>I had suspected the multiprocessing due to a similar <a href="https://github.com/imageio/imageio/issues/207" rel="nofollow noopener">issue someone else mentioned</a>, but they had resolved it by creating separate instances of the reader.</p>

SimonW: <p>It can be related indeed. Can you try num_workers = 0 and see if the issue still happens?</p>

mattrobin: <p>Yep, I’m currently running two machines with num_workers = 0 and 1 to see, but it will take an hour or so before they get around the number of steps it’s been consistently happening at.</p>
<p>Update: Got past 20,000 steps without issue. So it seems likely it is due to multiple readers reading from the same file. Thanks for you help!</p>
<p>Update 2: <code>num_workers</code> = 0 works. <code>num_workers</code> = 1 still results in the same error.</p>

smth: <p>if you are using python3, use the option (at the top of your main script, before you import imageio):</p>
<pre><code class="lang-auto">import torch
if __name__ == "__main__":
    import torch.multiprocessing
    import torch.multiprocessing.set_start_method("spawn")
</code></pre>
<p>I wonder if imageio is not fork-safe, and hence your error.</p>

mattrobin: <p>Sorry for the delayed testing of your suggestion.</p>
<p>Adding this in directly results in a <code>TypeError: can't pickle _thread.lock objects</code>. I suspected this was from the <code>imageio</code> reader, so I temporarily tried removing this from the my custom <code>Dataset</code> object. When doing so I get the following error:</p>
<pre><code>RuntimeError:
    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__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable.
</code></pre>
<p>Which is surprising, as the call you recommended was in a <code>__name__ == '__main__'</code> block and is being called on an Ubuntu OS (and from what I can find I should only expect this error on Windows).</p>

BBCraker: <p>Any update on this issue? <a class="mention" href="/u/smth">@smth</a> <a class="mention" href="/u/smth">@smth</a> <a class="mention" href="/u/mattrobin">@mattrobin</a>  Thanks in advance!</p>

BBCraker: <aside class="quote no-group quote-modified" data-username="mattrobin" data-post="9" data-topic="9629">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/a8b319/48.png" class="avatar"> mattrobin:</div>
<blockquote>
<p>TypeError: can’t pickle _thread.lock objects</p>
</blockquote>
</aside>
<p>I run into a similar issue in DDP where I loaded tfrecord dataset with more than 1 number_workds and I got the same error. TypeError: can’t pickle _thread.lock objects.<br>
Any update? Thanks in advance!</p>

wangjf8090: <p>I get the same problem when I was running two pytorch process for the same task , load different dataloader ( distinguish by diff name),  some error happened during the training like one dataloader caused 0 accuracy, it also happened when i use two dataloader of same file, the second dataloader will get 0 accuracy after model test. Maybe it’s because of the file_reader is the same one even used different dataloader? I can’t find the reason.</p>

wangjf8090: <p>update:<br>
when  i set the num_worker to 0, the two dataloader can work correctly.</p>