Wednesday, August 28, 2019

An Introduction to Asynchronous Programming. When and where to use Asynchronous Programming?


What is Asynchronous Programming?

When a computer is running a program, the operating system or the application allocates a group of resources; known as a thread; to run the application code.

In synchronous programming, all the code from on application is running on a single thread. Therefore, code has to execute synchronously which means code is executed in a one action at a time. One operation has to finish before the processor moves on to execute the next operation. That means if one operation is taking a long time, the program will freeze and stop waiting for it to finish before it moves to the next operation.

An asynchronous operation is an operation that runs on a separate thread that gets initiated from another thread. The thread that initiates an asynchronous operation does not need to wait for that operation to complete before it can continue. In that sense, resources can be allocated to other tasks that can be executed in the meantime.

Why use Asynchronous programming?

You might have already deduced this, asynchronous programming makes the best and most efficient utilization of the machine resources when it comes to having long running operations in an application. 

For a UI application, Asynchronous programming makes the application responsive because the UI thread is not blocked by a CPU or I/O heavy operation which makes it more interactive and user friendly. Asynchronous programming also takes advantage of parallel computing which is supported in most of today's machines. This helps programmers write parallel processing applications easily without having to write complicated code that is hard to maintain.

When to use Asynchronous Programming

As we mentioned, Asynchronous code is best used for long running operations. Those operations can be CPU bound or I/O bound operations. Examples of long running operations are:
  • I/O operations that include Network requests for data retrieval.
  • CPU heavy operations like scientific calculations using huge data sets.
  • I/O operations like disk access operations including reading and writing to disks.
If you have any I/O-bound needs (such as requesting data from a network or accessing a database), you'll want to utilize asynchronous programming so that your program UI does not freeze waiting for this operation to finish.

What applications are best candidates for asynchronous programming?

  • Desktop User Interface Applications, a desktop UI application is an application that is expected to be interactive, users should be able to interact and communicate with the various pieces of the application UI mostly at all times. Nothing is worse for an UI application than a frozen control that the user is unable to interact with because of a long running network operation like a web service call. The time spent waiting for information to travel across the network is blocking the UI resources and thus the application appears to be frozen or dead.

    Desktop / UI modern frameworks give special precedence to the thread that uses the UI. Async code is very important for these technologies to build better UI applications. Examples of UI technologies that use asynchronous programming are:
    • Windows Forms (WinForms)
    • Windows Presentation Foundation (WPF)
    • Universal Windows Platform (UWP)

  • Web Server Applications, a web server application does not deal with UI but often times, it needs to run remote database queries or run some calculations on large amounts of data to generate a data analysis report. Using Asynchronous programming for these tasks allows the server code to do the task efficiently specially that web servers are usually handling multiple requests from multiple clients at the same time. Asynchronous programming helps avoid situations where threads are simply waiting to do something while the rest of the application is getting overwhelmed by clients requests resulting in less than ideal performance as well as delayed responsiveness to clients requests.

Asynchronous Programming is not always the solution

There are problems that asynchronous code does not solve. There are also problems that Asynchronous code can cause. For example:
  1. Asynchronous code does not automatically improve performance. If a network task takes a certain amount of time, asynchronous code is not going to change that. In fact, there is some amount of (small) overhead for the framework managing the process. Rather, asynchronous code helps manage resources more efficiently.
  2. Using Asynchronous code imposes an overhead on the system. When threads are used, the system needs a way to manage them like thread saving, scheduling, listening, locking, resuming, ...etc. There are two types of overhead that exist when using threads, memory overhead which every thread reserves from a machine's virtual memory when initiated, and a scheduler overhead which is the way the operating system uses to manage choosing which thread should be executed on which CPU and when. These overheads can slow down the entire system if threads are excessively used.
  3. If you don't have a desktop / UI application, or your code is not network- or I/O-bound, you won't see much benefit.
  4. If your application is CPU-bound (it is slowing down because of heavy compute processes), multi-threading / task-based asynchrony is a better solution.


No comments:

Post a Comment

Please keep your comments relevant.
Comments with external links and adult words will be filtered.