This article will help you understand blocking and non-blocking in Nodejs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Recommended study: "nodejs Tutorial"
Everyone should have some ideas about the two concepts of blocking and non-blocking Understanding:
To put it simply, blocking is mostly caused by synchronous mode. Non-blocking can be understood as asynchronous mode processing some time-consuming operations.
So what are blocking and non-blocking in Node.js? How is blocking described? The following mainly explains this issue:
What is blocking in Node?
I/O operations can be understood as mainly referring to interaction with the system disk (data reading and writing) or network requests, etc.
Blocking is the execution of other JS codes You must wait until the previous time-consuming I/O operations
or some network requests are completed. Because Node has an event loop to solve this problem, if the event loop is not turned on during the execution of js, it will actually cause blocking.
In fact, in Node.js, there is also a situation of being blocked under normal circumstances. The reason is that Node's asynchronous processing is more friendly to I/O operations [using event loop], but for some CPUs that may exist in JavaScript Intensive operation performance is relatively low. Some students may ask, why can’t Node.js support CPU-intensive operations asynchronously? Because it can be understood that these CPU-intensive operations are actually some synchronous codes, such as a large number of for loops, massive data calculations, etc.
There are also some synchronization methods in the standard library of Node.js, most of which are based on libuv
to achieve blocking effects. There are also some blocking methods in Node's native modules. However, Node will also provide a corresponding asynchronous version of the API.
What is libuv doing? It is a library for asynchronous I/O operations that supports multiple platforms. For details, please see the official website: https://libuv.org/
What is non-blocking in Node?
First think about how to achieve non-blocking (asynchronous) in a context that is not limited to Node?
Simply two:
1) Multiple threads can be opened to handle concurrent operations
2) Event loop mode, if there are asynchronous operations placed in the event queue , after the asynchronous operation ends, call the corresponding callback function to process the asynchronous return result
Node.js is single-threaded. The reason is: the outer layer of Node.js is implemented by JavaScript, and the interpretation and execution of JavaScript is through V8 engine
is made.
Since the execution of JS is single-threaded, it is impossible for us to open another thread for interpretation and execution during JS interpretation and execution. Therefore, Node.js uses the second method to achieve non-blocking (asynchronous operation) )of.
Be careful not to mix blocking and non-blocking APIs in Node
English original address: https://nodejs.org/en/docs/guides/ blocking-vs-non-blocking/
For more programming-related knowledge, please visit: Programming Video! !
The above is the detailed content of Detailed explanation of blocking and non-blocking in Nodejs. For more information, please follow other related articles on the PHP Chinese website!

Vercel是什么?本篇文章带大家了解一下Vercel,并介绍一下在Vercel中部署 Node 服务的方法,希望对大家有所帮助!

gm是基于node.js的图片处理插件,它封装了图片处理工具GraphicsMagick(GM)和ImageMagick(IM),可使用spawn的方式调用。gm插件不是node默认安装的,需执行“npm install gm -S”进行安装才可使用。

今天跟大家介绍一个最新开源的 javaScript 运行时:Bun.js。比 Node.js 快三倍,新 JavaScript 运行时 Bun 火了!

在nodejs中,lts是长期支持的意思,是“Long Time Support”的缩写;Node有奇数版本和偶数版本两条发布流程线,当一个奇数版本发布后,最近的一个偶数版本会立即进入LTS维护计划,一直持续18个月,在之后会有12个月的延长维护期,lts期间可以支持“bug fix”变更。

大家都知道 Node.js 是单线程的,却不知它也提供了多进(线)程模块来加速处理一些特殊任务,本文便带领大家了解下 Node.js 的多进(线)程,希望对大家有所帮助!

node怎么爬取数据?下面本篇文章给大家分享一个node爬虫实例,聊聊利用node抓取小说章节的方法,希望对大家有所帮助!


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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),