Asynchrony (computer programming)

Asynchrony, in computer programming, refers to the occurrence of events independent of the main program flow and ways to deal with such events. These may be "outside" events such as the arrival of signals, or actions instigated by a program that take place concurrently with program execution, without the program blocking to wait for results.[1] Asynchronous input/output is an example of the latter cause of asynchrony, and lets programs issue commands to storage or network devices that service these requests while the processor continues executing the program. Doing so provides a degree of parallelism.[1]

A common way for dealing with asynchrony in a programming interface is to provide subroutines (methods, functions) that return to their caller an object, sometimes called a future or promise, that represents the ongoing events. Such an object will then typically come with a synchronizing operation that blocks until the operation is completed. Some programming languages, such as Cilk, have special syntax for expressing an asynchronous procedure call.[2]

Examples of asynchrony include the following:

  • Asynchronous procedure call, a method to run a procedure concurrently, a lightweight alternative to Threads.
  • "Ajax", short for "asynchronous JavaScript and XML")[3][4][5] is a set of web development techniques utilizing many web technologies used on the client-side to create asynchronous I/O Web applications.
  • Asynchronous method dispatch (AMD), a data communication method used when there is a need for the server side to handle a large number of long lasting client requests.[6] Using synchronous method dispatch (SMD), this scenario may turn the server into an unavailable busy state resulting in a connection failure response caused by a network connection request timeout. The servicing of a client request is immediately dispatched to an available thread from a pool of threads and the client is put in a blocking state. Upon the completion of the task, the server is notified by a callback. The server unblocks the client and transmits the response back to the client. In case of thread starvation, clients are blocked waiting for threads to become available.

Scenarios for async

  1. I/O operations: Examples are: making a network call, talking to a database, reading a file, printing a document, etc. A synchronous program that performs an I/O operation will come to a halt until the operation finishes. A more efficient program would instead perform the operation and continue executing other code while the operation is pending. Say you have a program that reads some user input, makes some computation and then sends the result via email. When sending an email, you have to send some data out to the network and then wait for the receiving server to respond. Time invested by waiting for the server to respond is time wasted that would be of much better use if the program continued computing.
  2. Performing multiple operations in parallel: When you need to do different operations in parallel, for example, making a database call, web service call and any calculations, then we can use asynchrony.
  3. Long-running event-driven requests: This is the idea where you have a request that comes in, and the request goes to sleep for some time waiting for some other event to take place when that event takes place, you want the request to continue and then send a response to client. So in this case, when request comes in, then thread is assigned to that request and as request goes to sleep, then thread is sent back to threadpool and as the task completes, then it generates the event and picks a thread from thread pool for sending response (the thread sent and picked from thread pool might or might not be the same.[7]

See also

References

  1. Davies, Alex (2012). Async in C# 5.0. O'Reilly. pp. 1–2.
  2. McCool, Michael; Reinders, James; Robison, Arch (2013). Structured Parallel Programming: Patterns for Efficient Computation. Elsevier. p. 30.
  3. Chris Shiflett. "Ajax Is Not an Acronym".
  4. "AJAX vs Ajax - Ajax ofcourse! (Arun Gupta, Miles to go ...)".
  5. Jesse James Garrett (18 February 2005). "Ajax: A New Approach to Web Applications". AdaptivePath.com. Retrieved 19 June 2008.
  6. ICE usage of AMD.
  7. Goel, Gaurav. "Async and Await Tutorial". Code Project. Retrieved March 3, 2020.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.