Home > Web Front-end > JS Tutorial > body text

Introduction to Node asynchronous I/O

不言
Release: 2018-07-11 15:44:09
Original
2492 people have browsed it

This article mainly introduces the introduction of Node asynchronous I/O, which has certain reference value. Now I share it with you. Friends in need can refer to it

Asynchronous IO

1. Why use asynchronous I/O

User experience

javascript is executed on a single thread, which is the same thread as the UI thread. If synchronization is used, the UI will be executed when javascript is executing. Rendering must stop and wait, which results in a very poor user experience.

If the web page needs to request some resources and obtain them synchronously, then we must wait for js to fully obtain the resources from the server before continuing to execute. During this period, the UI waits, which will cause the interaction with the user to Extremely poor, affecting the user experience.

// 现在请求两个资源
//耗时为M毫秒
getData('from_db');
//耗时为N毫秒
getData('from_remote_api');
Copy after login

If it is synchronous, it will take time(M N);
If it is asynchronous, it will take timeMax(M, N);

With the complexity of the application, the scenario will become M N... and Max (M, N,...) . At this time, the advantages and disadvantages of synchronization and asynchronous will become more prominent. On the other hand, as websites and applications expand, data is often distributed across multiple servers, and distribution means that the values ​​of M and N will grow linearly, which will also amplify the performance difference between asynchronous and synchronous. In short, IO is expensive, and distributed IO is even more expensive!

Resource allocation

Single-threaded synchronous IO

会因阻塞IO使得硬件资源无法得到更优的利用。
Copy after login

Multi-threaded programming

优点: 可以利用多核CPU有效提升CPU的利用率
缺点: 编程中的死锁、状态同步使得程序员很是头疼。
Copy after login

Asynchronous IO of node

node采用的异步IO,利用单线程,远离了多线程死锁、状态同步,利用异步让单线程远离了阻塞,使得CPU得到更好的利用。

为了弥补单线程无法利用多核CPU的问题,Node提供了子进程 `childProcess` ,将一些运算多的任务放入子进程进行高效的运算。
Copy after login

Introduction to Node asynchronous I/O

2. Blocking I/O and non-blocking I/O

Blocking IO

阻塞的IO操作就是发起IO操作后,线程阻塞等待IO完成,这期间cpu得不到有效利用。
Copy after login

Introduction to Node asynchronous I/O

Non-blocking IO

非阻塞IO操作其实就是发起IO操作后,通过事件轮巡,或者事件通知机制,不断查询IO操作是否完成,或者是主线程进入休眠等待事件通知IO结束,然后继续向下执行代码,实际上非阻塞IO期间,cpu要不用来查询要不用来休眠,也没有得到有效利用。依旧是同步IO。
Copy after login

Introduction to Node asynchronous I/O

3. Asynchronous I/O of node

A single link is required to complete the entire asynchronous IOEvent loop Observer Request object.

In fact, node’s asynchronous IO uses thread pool technology. When asynchronous IO is initiated, the io operation is thrown into the thread pool for execution, and then the main thread continues to perform other operations. The completion of io execution is notified through inter-thread communication. Main thread, the main thread executes the callback.

IO thread is implemented by Libuv (under Linux it is implemented by libeio; under window it is implemented by Controlled by the thread pool managed by IOCPspecific implementation), it is essentially multi-threaded. That is, thread pool and blocking IO are used to simulate asynchronous IO.

Introduction to Node asynchronous I/O

Asynchronous IO principle

When encountering IO, put it into an IO thread in the thread pool, let the The task is executed on the IO thread. The IO thread is executed in blocking IO mode, and then continues execution on the main thread. When another IO task is encountered, it is put into the thread pool, and then executed on another IO thread. Execution (also in blocking IO mode), the main thread continues to execute.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the analysis of Node module mechanism

The above is the detailed content of Introduction to Node asynchronous I/O. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template