In Java, there is a very useful construct called CountDownLatch to solve many multithreaded and asynchronous programming scenarios.
In tech terms it is simply
A synchronization aid that allows one or more threads to wait until a set of operations being performed in
other threads completes.
For example, you might have a third party API call which is asynchronous in nature. Many scenarios require that you call multiple of them and perform an action if all of them succeed OR perform another action even if one of them fails.
In the case below, we are using a popular message library called PubNub to send a message on a channel twice through the publish() method which is an async call
In the piece of code above, the thread blocks at latch.await() till both the API calls either succeeds or fails. This is preferable since it doesn’t uses Spin lock or Busy waiting
I was trying to do a similar thing in Python. Without using any multithreading construct, this is the version I came up with. Pretty lame it is
Here without using any threading constucts, we are using a while loop with a sleep which is busy waiting.