title: Python - How to schedule ThreadPoolExecutor?
body: <p>I'm using a <code>concurrent.futures.ThreadPoolExecutor</code> with a <code>Queue</code>, the code is something like this:</p>
  
  <pre><code>from concurrent.futures import ThreadPoolExecutor
  from queue import Queue
  
  def func(parent):
      return parent//2, parent//2, parent&lt;=2
  
  def worker(parent, q):
      child1, child2, end = func(parent)
      print(parent) 
      if not end:
          q.put(child1)
          q.put(child2)
  
  if __name__ == "__main__":
      q = Queue()
      q.put(100)
      executor = ThreadPoolExecutor(max_workers=6)
      while True:
          parent = q.get()
          future = executor.submit(worker, parent, q)
          if q.empty() and future.done():
              break
  </code></pre>
  
  <p>The problem with this code is that the <code>future.done()</code> is never True, and I cannot get out of this infinite <code>while</code> loop. </p>
  
  <p>My expected outcome is to wait until there's nothing to process. i.e. the queue is empty, and all worker have done their job, nothing further shall be put into this queue. Then I can stop this loop and do sth else.</p>
  
  <p>P.S. the actual <code>func</code> I'm using is more complex than the above example, but the problem is the same.</p>