title: How do I limit the number of active threads in python?
body: <p>Am new to python and making some headway with <code>threading</code> - am doing some music file conversion and want to be able to utilize the multiple cores on my machine (one active conversion thread per core).  </p>
  
  <pre><code>class EncodeThread(threading.Thread):
      # this is hacked together a bit, but should give you an idea
      def run(self):
          decode = subprocess.Popen(["flac","--decode","--stdout",self.src],
                              stdout=subprocess.PIPE)
          encode = subprocess.Popen(["lame","--quiet","-",self.dest],
                                  stdin=decode.stdout)
          encode.communicate()
  
  # some other code puts these threads with various src/dest pairs in a list
  
  for proc in threads: # `threads` is my list of `threading.Thread` objects
      proc.start()
  </code></pre>
  
  <p>Everything works, all the files get encoded, bravo! ... however, all the processes spawn immediately, yet I only want to run two at a time (one for each core).  As soon as one is finished, I want it to move on to the next on the list until it is finished, then continue with the program.</p>
  
  <p>How do I do this?</p>
  
  <p>(I've looked at the thread pool and queue functions but I can't find a simple answer.)</p>
  
  <p><strong>Edit:</strong> maybe I should add that each of my threads is using <code>subprocess.Popen</code> to run a separate command line <em>decoder</em> (flac) piped to stdout which is fed into a command line <em>encoder</em> (lame/mp3).  </p>