Skip to content

Instantly share code, notes, and snippets.

@iwanbolzern
Created January 22, 2020 07:17
Show Gist options
  • Save iwanbolzern/30ba5d2dadf80082de30063cff4fa0b4 to your computer and use it in GitHub Desktop.
Save iwanbolzern/30ba5d2dadf80082de30063cff4fa0b4 to your computer and use it in GitHub Desktop.
Simple code snipped for adding a done callback to multiple features ✨

How to use it

The example bellow shows how to execute 3 async functions and after all are finished, final_function() is called.

def final_function():
  print('All features done')

with ThreadPoolExecutor() as executor:
  future_1 = executor.submit(do_something)
  future_2 = executor.submit(do_something)
  future_3 = executor.submit(do_something)
  
  multi_feature_done_callback([future_1, future_2, future_3], final_function)
def multi_feature_done_callback(futures: List[Future], final_cb: Callable):
head = futures.pop(0)
if len(futures) <= 0:
head.add_done_callback(lambda future: final_cb())
return
head.add_done_callback(lambda _: self._wait_future_async(futures, final_cb))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment