title: multiprocessing in IPython console on Windows machine - if __name_ requirement
body: <p>I'm working with IPython and Spyder IDE on a Windows machine. When the IDE is starting, a set of py-files is loaded to define some functions that make my work a bit easier. Everything works as expected.</p>
  
  <p>Now I would like to upgrade one of these function to use multiprocessing, but on Windows this requires the <code>if __name__ == "__main__":</code> statement. So it seems that I cannot call the function directly and pass the arguments from the IPython console.</p>
  
  <p>For example one of the py-files (let's call it test.py) could look like the following code.</p>
  
  <pre><code>import multiprocessing as mp
  import random
  import string
  
  # define a example function
  def rand_string(length, output):
      """ Generates a random string of numbers, lower- and uppercase chars. """
      rand_str = ''.join(random.choice(
                  string.ascii_lowercase
                  + string.ascii_uppercase
                  + string.digits)
             for i in range(length))
      output.put(rand_str)
  
  
  def myFunction():
      # Define an output queue
      output = mp.Queue()        
  
      # Setup a list of processes that we want to run
      processes = [mp.Process(target=rand_string, args=(5, output)) for x in range(4)]
  
      # Run processes
      for p in processes:
          p.start()
  
      # Exit the completed processes
      for p in processes:
          p.join()
  
      # Get process results from the output queue
      results = [output.get() for p in processes]
  
      print(results)
  </code></pre>
  
  <p>In my IPython console I would like to use the line </p>
  
  <pre><code>myFunction()
  </code></pre>
  
  <p>to trigger all the calculations. But on Windows a end up getting a BrokenPipe error. </p>
  
  <p>When I put </p>
  
  <pre><code>if __name__ == "__main__":
       myFunction()
  </code></pre>
  
  <p>at the end of the py-file and run the complete file by</p>
  
  <pre><code>runfile(test.py)
  </code></pre>
  
  <p>it works. Of course. But that makes it very hard to pass arguments to the function as I always have to edit the test.py-file itself.</p>
  
  <h2>My question is: How do I get the multiprocessing function running without putting it in this <code>if __name__ == "__main__":</code> statement??</h2>