NodeJs asynchronous

Verbatim https://www.dazhuanlan.com/2019/08/25/5d625d714f62a/


This article focuses on issues NodeJs in asynchronous. Use NodeJs writing process is also more than a year, when the company internship, company background are two projects I am responsible use NodeJs and Sails framework written. I mainly work mode to provide RESTful interfaces, web client on the server to get data server via AJAX way. In the process of doing, encountered many problems, the deepest feelings are asynchronous programming habit of thinking is not adapted very well.

Asynchronous background

In fact, asynchronous very early have a concept such as the operating system there is asynchronous. Later, with the popularity of Ajax technology, most front-end developers to understand the concept of asynchronous. With the introduction of the V8 Google's Chrome browser, the emergence of Nginx server asynchronously processing network requests, time-proven, concurrent performance of Nginx better than Apache, Tomcat and so on. After the introduction of V8, Ryan Dahl introduced the NodeJs, the first fully supported NodeJs of technology in language platform. Of course, Java, PHP and other programming also supports asynchronous mode, but it is implemented on the communication between multiple threads and threads through on the basis of the Java language. And NodeJs on the implementation language is thought to asynchronous basis.

Asynchronous implementation

Talk about asynchronous, you have to understand blocking and non-blocking IO IO.
Blocking IO we well understood, because in normal programming, we often read and write documents, such as in the C language we use open function to open a large document, so this time the process will be to block until the document has been read the subsequent operation to continue.

Non-blocking IO is to use another thread to read large documents, subsequent operation of the main thread can not wait to read threads continue to run. But this time we do not know when to read threads completion operations in the main thread. So we are using polling techniques, each time interval, go read the thread to see whether the operation is completed. In Linux, this polling can read, poll, epoll, kqueue of ways, but the polling technique has the disadvantage that, when polled, the main thread must wait for the CPU up empty.

The best non-blocking IO is the main thread and the thread basic reading without disturbing each other only when reading thread after the operation is completed, will notify the main thread has been read. NodeJs achieved by asynchronous libuv, and libuv will be used to achieve a corresponding manner the operating system platform. As in Linux, the thread pool by way of realization is achieved by calling the IOPC the Windows platform, in fact IOPC also use the thread pool of ways, but realized only in the Windows kernel.

Overall, NodeJs say though is single-threaded, asynchronous, event-driven, but I mainly want to talk about here, nodejs is achieved through a multi-threaded asynchronous way. We are talking about just a point to single-threaded code running in the main thread NodeJs only, asynchronous operations are completed by the underlying thread pool.

Of course, NodeJs is asynchronous implementation, then we in the preparation of the application process, to try to use asynchronous mode - callback, so our application process in order to have high performance asynchronous, or synchronous manner to write might as well use.

NodeJs multi-process (thread) method

NodeJs is not to say that do not support single-threaded multi-process technology, we can still use NodeJs of child_process to achieve multi-process operation, which with the Linux fork, exec , etc. the same, of course, if you do not want to use such a primitive way multi-process, then you can NodeJs of cluster simple implementation module.

Reference material

In layman's language nodejs - Chapter III

In layman's language nodejs - Chapter IX

NodeJs child_process

NodeJs cluster

Guess you like

Origin www.cnblogs.com/petewell/p/11408889.html