title: Waiting for executoro to finish while tasks can submit more tasks
body: <p>I submitting tasks to a ThreadPoolExecutor, while those tasks can submit more tasks in some cases. I want to wait until all tasks are finished. I have a solution for that using a <code>condition</code> over the internal queue size of the <code>executor</code>, but though it works, I feel it is not the best approach.</p>
  
  <p>Here is an example for what I'm doing, I reduced it to a very simple case:</p>
  
  <pre><code>from concurrent.futures.thread import ThreadPoolExecutor
  from threading import Condition
  
  
  def func(e: ThreadPoolExecutor, c: Condition, val: int):
      print(val)
      if val &lt; 5:
          # A case when I need to submit a new task
          e.submit(func, e, c, val + 1)
      with c:
          c.notify()
  
  
  with ThreadPoolExecutor(max_workers=5) as e:
      c = Condition()
      for i in range(10):
          print(e._work_queue.qsize())
          e.submit(func, e, c, 1)
      with c:
          while not e._work_queue.qsize() == 0:
              c.wait_for(lambda: e._work_queue.qsize() == 0)
  </code></pre>
  
  <p>Is there a better and/or cleaner way to do that?</p>