title: How to use multiprocessing in a for loop
body: <p>I am new to Python <code>multiprocessing</code>. I have a function that returns values and is supposed to act in parallel. In the following, you can find a sample code.</p>
  
  <pre><code>import multiprocessing as mp
  from tqdm import tqdm
  
  def foo(self):
      arg_triplets = [(self.loc_x[ii], self.loc_y[jj], self.arg) 
                      for ii in np.arange(0, self.nx) for jj in np.arange(0, self.ny)]
      ctx = mp.get_context('fork')
      max_proc = mp.cpu_count()-1
      pool = ctx.Pool(processes=max_proc)
      return_values = list(tqdm(pool.imap(target_foo, arg_triplets), total=nx*ny))
      pool.close()
      pool.join()
  </code></pre>
  
  <p>So, when I run this routine once, everything works fine. The function <code>target_foo</code> takes triplets of arguments and returns all the output values as a list. I can monitor the status of my 8 core processor, and see 7 of them are working simultaneously. But the problem starts when I use function <code>foo</code> in a <code>for</code> loop. For example, I need to gather data for multiple <code>foo</code> which I do not need in parallel. So I create a <code>for</code> loop that calls <code>foo</code> sequentially. In each call of <code>foo</code>, the function <code>target_foo</code> is supposed to work in parallel. The problem is that for the first time it works on parallel, but from the second time, it does not. What am I doing wrong?</p>