title: Why is it important to protect the main loop when using joblib.Parallel?
body: <p>The joblib docs contain the following warning:</p>
  
  <blockquote>
    <p>Under Windows, it is important to protect the main loop of code to
    avoid recursive spawning of subprocesses when using joblib.Parallel.
    In other words, you should be writing code like this:</p>
  
  <pre><code>import ....
  
  def function1(...):
      ...
  
  def function2(...):
      ...
  
  ... if __name__ == '__main__':
      # do stuff with imports and functions defined about
      ...
  </code></pre>
    
    <p>No code should run outside of the “if __name__ == ‘__main__’” blocks,
    only imports and definitions.</p>
  </blockquote>
  
  <p>Initially, I assumed this was just to prevent against the occasional odd case where a function passed to <code>joblib.Parallel</code> called the module recursively, which would mean it was generally good practice but often unnecessary. However, it doesn't make sense to me why this would only be a risk on Windows. Additionally, <a href="https://stackoverflow.com/a/21029356/3204472">this answer</a> seems to indicate that failure to protect the main loop resulted in the code running several times slower than it otherwise would have for a very simple non-recursive problem.</p>
  
  <p>Out of curiosity, I ran the super-simple example of an embarrassingly parallel loop from the joblib docs without protecting the main loop on a windows box. My terminal was spammed with the following error until I closed it:</p>
  
  <pre><code>ImportError: [joblib] Attempting to do parallel computing without protecting your import on a system that does not suppo
  rt forking. To use parallel-computing in a script, you must protect you main loop using "if __name__ == '__main__'". Ple
  ase see the joblib documentation on Parallel for more information
  </code></pre>
  
  <p><strong>My question is,</strong> what about the windows implementation of joblib requires the main loop to be protected in every case?</p>
  
  <p>Apologies if this is a super basic question. I am new to the world of parallelization, so I might just be missing some basic concepts, but I couldn't find this issue discussed explicitly anywhere.</p>
  
  <p>Finally, I want to note that this is purely academic; I understand why it is <a href="https://stackoverflow.com/questions/419163/what-does-if-name-main-do">generally good practice</a> to write one's code in this way, and will continue to do so regardless of joblib.</p>