title: Using python multiprocessing Pool in the terminal and in code modules for Django or Flask
body: <p>When using multiprocessing.Pool in python with the following code, there is some bizarre behavior.</p>
  
  <pre><code>from multiprocessing import Pool
  p = Pool(3)
  def f(x): return x
  threads = [p.apply_async(f, [i]) for i in range(20)]
  for t in threads:
      try: print(t.get(timeout=1))
      except Exception: pass
  </code></pre>
  
  <p>I get the following error three times (one for each thread in the pool), and it prints "3" through "19":</p>
  
  <pre><code>AttributeError: 'module' object has no attribute 'f'
  </code></pre>
  
  <p>The first three apply_async calls never return.</p>
  
  <p>Meanwhile, if I try:</p>
  
  <pre><code>from multiprocessing import Pool
  p = Pool(3)
  def f(x): print(x)
  p.map(f, range(20))
  </code></pre>
  
  <p>I get the AttributeError 3 times, the shell prints "6" through "19", and then hangs and cannot be killed by [Ctrl] + [C]</p>
  
  <p>The multiprocessing docs have the following to say:</p>
  
  <blockquote>
    <p>Functionality within this package requires that the <strong>main</strong> module be
    importable by the children.</p>
  </blockquote>
  
  <p>What does this mean?</p>
  
  <p>To clarify, I'm running code in the terminal to test functionality, but ultimately I want to be able to put this into modules of a web server.  How do you properly use multiprocessing.Pool in the python terminal and in code modules?</p>