title: Python ThreadPool with limited task queue size
body: <p>My problem is the following: I have a <code>multiprocessing.pool.ThreadPool</code> object with <code>worker_count</code> workers and a main <code>pqueue</code> from which I feed tasks to the pool. </p>
  
  <p>The flow is as follows: There is a main loop that gets an item of <code>level</code> level from <code>pqueue</code> and submits it tot the pool using <code>apply_async</code>. When the item is processed, it generates items of <code>level + 1</code>. The problem is that the pool accepts all tasks and processes them in the order they were submitted. </p>
  
  <p>More precisely, what is happening is that the <code>level 0</code> items are processed and each generates 100 <code>level 1</code> items that are retrieved immediately from <code>pqueue</code> and added to the pool, each <code>level 1</code> item produces 100 <code>level 2</code> items that are submitted to the pool, and so on, and the items are processed in an BFS manner. </p>
  
  <p>I need to tell the pool to not accept more than <code>worker_count</code> items in order to give a chance of higher level to be retrieved from <code>pqueue</code> in order to process items in a DFS manner. </p>
  
  <p>The current solution I came with is: for each submitted task, save the <code>AsyncResult</code> object in a <code>asyncres_list</code> list, and before retrieving items from <code>pqueue</code> I remove the items that were processed (if any), check if the length of the <code>asyncres_list</code> is lower than the number of threads in the pool every 0.5 seconds, and like that only <code>thread_number</code> items will be processed at the same time.</p>
  
  <p>I am wondering if there is a cleaner way to achieve this behaviour and I can't seem to find in the documentation some parameters to limit the maximum number of tasks that can be submitted to a pool.</p>