search
HomeWeb Front-endJS TutorialIntroduction to Node asynchronous I/O

Introduction to Node asynchronous I/O

Jul 11, 2018 pm 03:44 PM
node.js

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');

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使得硬件资源无法得到更优的利用。

Multi-threaded programming

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

Asynchronous IO of node

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

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

Introduction to Node asynchronous I/O

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

Blocking IO

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

Introduction to Node asynchronous I/O

Non-blocking IO

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

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!

Statement
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
Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment