Asynchronous programming: types, classification, principles of programming, concept, meaning and application

Asynchronous programming (AP) is a form of parallel programming that allows the structural unit of the system to work separately from the main application flow. When a job is completed, it notifies the main thread of whether the job was completed or not. Such programming is advantageous because it increases the supported bandwidth, which makes it attractive, given the growing demand of the Internet for systems with high scalability.

Asynchronous Programming Models

In order to process the continuity of the result of non-blocking operations after their completion, various AP models were created. Their advantages are evaluated in terms of how close they are to the scheme closest to the sequential one.

Asynchronous Programming Models




Types of AP models:

  1. Continuations step model - the successive step model. This is the most used asynchrony in Node JS. Each function receives information about how it should process the result of success or error in the operation.
  2. Event model - an event model that uses an event-driven architecture that allows non-blocking operations to report completion upon signs of success or failure, requires correlation to synchronize.
  3. Promise model - a promise model, explained by the return values ​​of non-blocking operations, regardless of the point in time at which the specified success or failure values ​​were received.
  4. Generator model - generator model. Generators are used to temporarily return control to the calling program and then return to the subprogram by restoring the state to the point at which it was stopped.

Architectural Principles of Node

Despite the fact that recently Node JS has received sharp criticism regarding its use of computational cycles due to a single-threaded environment, its philosophy based on three strong architectural principles remains in demand.





JavaScript is asynchronous in nature, as is Node. The platform for running server-side JavaScript, Node.js, was introduced in 2009 using an event-driven asynchronous I / O model, which makes it efficient and scalable.

Chat is the most typical real-time Node.js multi-user application. Starting with IRC for many proprietary and open protocols on non-standard ports, it became possible to implement everything in modern Noje.js with WebSockets, which work by default on the same port 80, which listens for new messages sent by their clients. On the client side there is an HTML page with several configured handlers, one for the Send button, which selects a message and sends it to WebSocket, and the other that listens for messages arriving at the client. Obviously, this is a simple and basic model, but based on a different variance of complexity.

The inactive model that Node JS uses in the API to support asynchronous programming is a step to continue. Each non-blocking operation receives a function as the last parameter, which includes continuation logic. It will be called after the operation is completed, both to process the results if successful, and to eliminate errors. The continue function allows you to specify the lock operation, how it should continue after the operation is completed.

Architectural Principles of Node




Serial flow control

In order to continue the establishment of successive execution flows within the framework of this model, it is necessary to combine each subsequent function in a chain, as a continuation of the previous one, where the results will be processed in case of success or failure. This leads to the diagonalization of the code, which was called the pyramid of hell (callback hell), due to the lack of practical controllability, if only the number of consecutive chains grows minimally.





Serial flow control




Parallelization - asynchronous execution of non-blocking operations occurs immediately, since its simple call is performed in the background by definition. To turn blocking operations into non-blocking operations, a small encapsulation process is required that starts the operation in the background.

asynchronous non-blocking operations




Continuation Function Sync

It requires a chain at the end of each parallel sequence of the completion function, which applies certain logic only after it is confirmed that all parallel branches are complete. To implement this check, diagrams based on counters are used.

Continuation Function Sync




An example using continuers:

  1. Parallelism, the loop allows you to perform all non-blocking read-account pairs in a non-blocking way.
  2. A sequence, each read pair is read through a step of the continuation functions.
  3. Synchronization, each parallel branch receives the last continuation, which executes the completion logic as soon as the completion of all branches is ensured.

Successor Libraries

There are many libraries that can help simplify the life of developers working with the AP model. Some of them are associated not only with AP, but also with the functional paradigm, which is due to the fact that the mechanisms for introducing compounds, in fact, are functional advantages.

Types of Libraries:

  1. Async is perhaps the most famous and widely used library for successor-based asynchronous programming. It offers various flow control methods for non-blocking functions.
  2. Join - is an implementation of the synchronization method, which can be found in other languages, such as C and working with streams. It can also be used in promises, although in this case its use is less relevant.
  3. Fn.js is a great library that implements various functional management methods. Its practical applicability in this context is related to the capabilities it provides for generating non-blocking functions and applying validation.

Advantages and disadvantages

Continuity-based asynchronous programming is a good option for situations with simple flow control logic. This usually refers to programs in Node JS that allow you to define a non-blocking response to incoming requests.

Advantages of the model:

  1. Simple request and response schemes.
  2. Consistency with functional programming schemes.
  3. Easy to understand as a conceptual mechanism.

Disadvantages:

  1. When the control logic is not well developed, the process gets complicated, which leads to the creation of code with distributed functional logic that is difficult to read, understand and maintain.
  2. Difficulty in defining flow control logic.
  3. Difficultly tuned synchronization mechanisms.
  4. The control logic is distributed between each non-blocking branch.

Event Driven Model

An event is a signal in the business ecosystem. Anatomically, they usually consist of a type, a timestamp, and a dataset describing the context in which the event occurred. Event Architecture (EDA) provides a mechanism for communication between customers and suppliers in a 1: N relationship and with a nominal isolation. One of its many uses is troubleshooting asynchronous data processing.

In event-driven centralized architectures, there is a central intermediary communication bus that is responsible for ensuring the efficiency of the process of registering listening clients and triggering notifications upon request from suppliers. This mechanism allows for N: N power, and the circuit is called the PUB / SUB pattern.

In distributed event-driven architectures, each provider is responsible for managing the subscription of their customers and for sending notifications when an event occurs. The communication mechanism is also nominally separated, but usually the number of providers and clients is 1: N. This pattern corresponds to the observed pattern or event sources in Node JS jargon.

Computational Abstraction: Promise

Computational Abstraction: Promise




A promise is a computational abstraction, which is a commitment on the part of a non-blocking operation called to deliver a response to the calling program when the result is received after completion. A promise is an object that provides two methods to enable processing logic in case of success or failure.

They correspond to a simple life cycle that you need to know in order to work with them. The essential value of a promise lies in two principles. Firstly, the logic of the process in case of success or failure is applied only once. And secondly, the logic of success or failure is guaranteed to be fulfilled, even if the promise is resolved before its drivers are introduced. If required, a promise awaits its handlers, asynchronous JavaScript programming.

There are several ways to get promises that can be identified as building patterns that appear periodically when using this model. The definition of ES6 includes promises and Node JS version 0.12 has support for this specification. In addition, there are several libraries that implement the promise model. In order to have a comparative reference system, the standard Promises A + was defined, which manages all implementations with objects available at that time.

Synchronous and asynchronous programming

It is recommended that you use the JavaScript API, loading for all of its tags and the vendor code from it. This ensures the best compatibility with the largest number of suppliers. This placement provides all vendor tags with the greatest opportunity to complete the tracking before the visitor moves to the next page.

Synchronized loading occurs when the browser needs to stop rendering a page in order to complete the execution of JavaScript code. If it detects a synchronous JS tag, it blocks the display of the page until the code completes. This is similar to a slow-moving truck on a single lane road that slows down behind it. Modern websites have moved away from this method because it poses a direct risk of delayed page load times.

The disadvantage of this method is that the entire site is blocked from the beginning until the tag is fully loaded. Although tag providers enter into service level agreements over the course of their delivery, several factors can affect performance. These include slow response times associated with suppliers, maintaining unnecessary application servers in a hybrid client-server model, and slow Internet traffic. If a user downloads tags synchronously, it is recommended that the provider has a response time of 100 milliseconds (ms) or faster.

Programming synchronous and asynchronous data processing via APIs are application programming interfaces that return data for requests either immediately or, respectively, later. Synchronous and asynchronous APIs provide a way to make immediate or scheduled requests for resources, data, or services when available. The modern method adopted for sites is to asynchronously load tags.

In this method, JavaScript code is processed in parallel with the rest of the page. This means that even if the vendor tag responds slowly or loads, it will not slow down the rest of the page. Using this approach, you can not only separate JavaScript tags that are loaded independently, the asynchronous method minimizes the impact of loading external JS files on the page rendering process.

Understanding and profiling C #

Understanding and profiling C #




Microsoft and the .NET community have greatly simplified the AP by implementing asynchronous wait in C #. Recent versions of ASP.NET are actively using it to improve performance. Many tools for monitoring performance and profiling are trying to maintain and visualize the performance of 1C asynchronous programming. Stackify Prefix & Retrace products have excellent support for applications using C # async await. To get started, you need to understand how code that uses async awai "and HttpClient as an example actually works.

Understanding and profiling C #




Using ILSpy, you can see how the compiler converts this code to AsyncState Machine. The state machine executes all the complex code undercover, which allows developers to write asynchronous code.

HttpClient as an example




Profiling asynchronous programming in C 5 0 is tricky because it crosses threads. Traditionally, a method and all calls to its child methods occur in a single thread. This makes it easier to understand the relationship between parent and child methods. With asynchronous code, this is a completely different story. The parent method runs in a single thread. When an I / O operation begins, the code in this thread ends. When the I / O operation completes, the code continues to execute on the new thread. Linking code between these threads as part of a larger transaction is quite difficult.

AIOHTTP: client server for asyncio

Aiohttp - allows users to create asynchronous servers and clients. The aiohttp asynchronous programming package works for client and server web sockets. The documentation from this aiohttp example is used to capture an HTML page.

AIOHTTP: client server for asyncio




This example shows how to download one or more files, and you can also download files through the program. It points out several new elements, such as asynctimeout. This allows you to create a timeout context manager. Below, the code creates an asynchronous synchronization cycle and uses it as the main function.

They create a Client Session object in the main function of asynchronous programming and coroutine, and a coroutine function that collects the URL of everything that needs to be loaded. In download coroutine, he creates a context manager that runs for about X seconds. After this number of seconds, X, the context manager ends. Next, use the get () function of the session, which finds the response object.

When the developer creates the content attribute of the response object, it returns aiohttp. StreamReader, which allows the user to upload a file in any size. As soon as the file is read, it will be written to the local drive. Then use the response () function to complete the response processing. According to the documentation, it implicitly calls release (). However, asynchronous Python programming is clearly better. It is better to leave this function to prevent further problems. There is one section that blocks the section of code written to disk, while the code remains locked. Using aiohttp is a real way to improve your workflow, where users don’t have to spend time creating a server, downloading links and writing asynchronous files, which reduces the time it takes to create a project.

Asynchronous programming allows you to achieve greater efficiency in software, because the flow of execution for long processes or user interaction is not blocked, both for developing applications for Node and for browsers.




All Articles