Async in a nutshell : Get into Asynchronous

Buddhika Chathuranga
RuntimeError
Published in
4 min readOct 6, 2019

This is the era of information. There is a vast amount of data available now to process and create information to make good decisions and the processor has become the most valuable thing inside a computer. So, it is not a good habit to write your code in a processor wasting manner. Since more processing power is utilizing more energy, programmers have a huge responsibility to write their code to save the world.

This is where asynchronous becomes involved. Asynchronous programming helps to write codes in an efficient manner so that it won’t waste the processing power as well as save the world. Before getting into asynchronous we must have a good understanding of synchronous programming as well. In-order to understand synchronous, imagine a computer system as a restaurant. The processor is the chef in this restaurant. So, the manager has a responsibility to manage the whole process so that the chef can work as much as possible. You are the manager and the code is the whole process of the restaurant.

Think an order comes to the chef and the order is a coffee, egg, and toast. Now there are several procedures which the chef can use to finish this order. First, he can cook the egg. After the egg is cooked, he can start cooking the toast. Until the eggs get cooked, he is waiting. Doing nothing. After the toast gets cooked, he can start making coffee. Until the toast gets cooked, he does nothing. This is one way that chef can complete the order. For a better understanding refer to the image below.

The total time that the chef takes to complete the order is the sum of the time of all three tasks. But here we can see there is a lot of time that chef can save with some different workaround. He can start cooking toast until eggs are cooking. But I’ll come to that later. There is another workaround that manager can take. He can hire another two chefs. Since there are three chefs now, they can start making coffee, cooking toast and cooking eggs at once. So, the total time that takes to complete order is the maximum time among all three tasks. Obviously, it’s much less than the first time. But here, the manager must pay some extra amount for the newly hired chefs. So, it’s not an efficient workaround. Refer to the image below.

There is another workaround and obviously that is the way that all chefs follow in real life. The chef can start cooking egg and until eggs cooking he can start cooking toast. While the eggs and toast are getting cooked he can busy himself with other stuff like cleaning the kitchen, getting new orders or simply he can wait. When the toast and eggs are about to be cooked he can start making the coffee and serve. So in this way, he can speed up his work and also the manager does not need to pay extra allowances for the chefs. So this is the asynchronous way of getting things done. For more understanding refer to the image below.

Now let us try to map these scenarios into computer programs. Before that let us try to understand how computer program works. Imagine a simple mobile application. It keeps asking “did you say anything, did you say anything”. It is looking for user to say something. That something is an event. If we press a button, the application responds to that. If we press another button then the application responds to that.

Imagine we have a mobile application which gets some data from a database when we press a button, and then populates that on to the screen. What if that database request takes a long time? Then it will take some time until that data is populated on the screen. But what if we can’t do anything in the application like minimize, scroll up and down until that request gets finished? It won’t be good user experience. But since there is a single processor on the mobile phone (This is not true, I’ll talk about this later) like we had one single chef at the restaurant, the user has to wait without doing anything on that application until that request gets finished. But that is not true. In almost all the good mobile applications that we have used, the application can still respond to our events. So how can an application achieve that? That’s where asynchronous programming becomes important.

In the next article I will talk about how asynchronous happens inside a computer program. If you are interested, you can find the next article Why developers should write processor killing codes here.

--

--