title: Parallelizing bulk of functions on multiple cpu environment
body: <p>I want to create a function that, given a list of functions and relative arguments, starts as much processes as possible in order to parallelize those tasks. The number of running processes can't be more than the number of cores of my CPU. When a process finishes, it should be replaced with another one, until the end.</p>
  
  <p>I tried to implement such a thing using python Pools. Here's my function:</p>
  
  <pre><code>from multiprocessing import Pool, cpu_count
  
  CPUS = cpu_count()
  
  def parallelize(functions, args):
      results = []
      if CPUS &gt; 1:
          for i in xrange(0, len(functions), CPUS):
              pool = Pool()
              for j in xrange(CPUS):
                  if i + j &gt;= len(functions):
                      break
                  results.append(pool.apply_async(functions[i + j], args = args[i + j]))
              pool.close()
              pool.join()
          map(lambda x: x.get(), results)
      else:
          for i in xrange(len(functions)):
              results.append(functions[i](*args[i]))
      return results
  </code></pre>
  
  <p>This implementation subdivides the list of functions in bulks. Each bulk dimension is equal to the number of the actual CPUs. The problem is that it actually waits until each bulk of functions finishes, and then starts again another bulk of processes.<br>
  I don't want this behavior because if there was a very slow function in a bulk, the other cpus will wait for it to finish before starting new processes.</p>
  
  <p>What is the correct approach?</p>