node.js非阻塞io
In this article, we try to provide you a detailed illustration of the difference between blocking and non-blocking calls in Nodejs. This blog will refer to the event loop and libuv.
在本文中,我们试图为您详细说明Node.js中阻塞和非阻塞调用之间的区别。 该博客将引用事件循环和libuv。
NODEJS中的阻塞和非阻塞 (BLOCKING AND NON-BLOCKING IN NODEJS)
A blocking or non-blocking operating model is a process that deals with input/output operations.
阻塞或非阻塞操作模型是处理输入/输出操作的过程。
Reading from a source or writing to a resource is considers as an I/O operations. “I/O” refers to the interaction with the system’s disk and network supported by libuv module.
从源读取或写入资源被视为I / O操作。 “ I / O”是指与libuv模块支持的系统磁盘和网络的交互。
On the other hand, the random-access memory (RAM) has the ability to access files from any location in the same amount of time irrespective of its physical location. This memory can be addressed quickly and doesn’t require waiting. So accessing memory is not considered as an I/O operation.
另一方面,随机存取存储器(RAM)能够在相同的时间内从任何位置访问文件,而不管其物理位置如何。 此内存可以快速解决,不需要等待。 因此,访问内存不被视为I / O操作。
The most common tasks carried out by most of the developers is File I/O, this fundamental process has three processes:
大多数开发人员执行的最常见任务是文件I / O,此基本过程包括三个过程:
- Open a File. 打开一个文件。
- Read its contents and print. 阅读其内容并打印。
- Executing something. 执行某件事。
封锁 (BLOCKING)
A Blocking term is originally originated from the operating system process model. It is actually a multitasking operating system, each labeled process with a state depending on how ready those processes are to be put on the CPU for execution.
阻止项最初源自操作系统过程模型。 它实际上是一个多任务操作系统,每个标记的进程的状态取决于将这些进程放置在CPU上执行的准备程度。
When JavaScript execution in the Nodejs process has to wait until a Non-JavaScript operation to complete is called blocking.
当Nodejs进程中JavaScript执行必须等到非JavaScript操作完成时,称为阻塞。
A process is labeled as blocked if it is not ready for execution. So it will wait for an event to occur. Each Input/Output event indicates either progress or completion in an I/O operation.
如果尚未准备好执行进程,则将其标记为已阻止 。 因此它将等待事件发生。 每个输入/输出事件都表示I / O操作的进度或完成。
Performing a blocking system call forces the entire process to enter into the blocked state.
执行阻塞系统调用会强制整个过程进入阻塞状态。
Let’s see how this can be done with blocking code in Nodejs:
让我们看看如何使用Nodejs中的阻塞代码来完成此操作:
Here we try to read simple files: hosts and users and printing their contents, meanwhile printing a few welcome messages.
在这里,我们尝试读取简单的文件:主机和用户并打印其内容,同时打印一些欢迎消息。
阻止/同步代码: (BLOCKING/SYNCHRONOUS CODE :)
Contents of the hosts file :
主机文件的内容:
190.158.0.1173.0.1.1205.250.255.0
Contents of users file :
用户文件内容:
paulsmithdogSnakevar fs = require('fs'); var contents = fs.readFileSync('users','utf8'); console.log(contents); console.log("Welcome Node\n"); var contents = fs.readFileSync('hosts','utf8'); console.log(contents); console.log(“Welcome again!”);
Let’s see the output down here for Blocking/Synchronous code :
让我们在这里查看阻塞/同步代码的输出:
paulsmithdogSnakeWelcome Node190.158.0.1173.0.1.1205.250.255.0Welcome again!
By relating to the output code, we may come to know about some major points:
通过与输出代码相关,我们可能会了解一些要点:
- It’s a blocking code 这是一个阻止代码
- We see the contents of the user file, the ‘Welcome Node’ strongly support the above point. 我们看到了用户文件的内容,“ Welcome Node”强烈支持以上几点。
- The same happens with the next file. 下一个文件也是如此。
非阻塞 (NON BLOCKING)
A non-blocking operation in Node js does not wait for I/O to complete. Whenever a blocking operation happens in the process, all other operations will put on hold at the operating system level. Performing such non-blocking I/O operation, the process continues to run in the non-blocking mechanism.
Node js中的非阻塞操作不会等待I / O完成。 每当进程中发生阻塞操作时,所有其他操作都将在操作系统级别被暂停。 执行这种非阻塞I / O操作后,该过程将继续以非阻塞机制运行。
A non-blocking call initiates the operation and leaves it for OS to complete returning immediately without any results.
非阻塞调用将启动该操作,并将其留给OS以便立即完成返回而没有任何结果。
Let’s see how this can be done with non-blocking code in Nodejs:
让我们看看如何使用Node.js中的非阻塞代码来完成此操作:
Here we try to read simple files: hosts and users and printing their contents, meanwhile printing a few welcome messages.
在这里,我们尝试读取简单的文件:主机和用户并打印其内容,同时打印一些欢迎消息。
Read: What is callback function in Node.js?
非阻塞/异步代码: (NON BLOCKING/ASYNCHRONOUS CODE:)
Contents of the host’s file :
主机文件的内容:
190.158.0.1173.0.1.1205.250.255.0
Contents of users file :
用户文件内容:
paulsmithdogSnakevar fs = require('fs');var contents = fs.readFile('./users','utf8', function(err,contents){ console.log(contents);});console.log("Welcome Node\n"); var contents = fs.readFile('./hosts','utf8', function(err,contents){ console.log(contents);});console/log(“Welcome again”);
Let’s see the output down here for Non-blocking or Asynchronous code :
让我们在下面查看非阻塞或异步代码的输出:
Welcome NodeWelcome again!paulsmithdogSnake190.158.0.1173.0.1.1205.250.255.0
By comparing with the previous sync code output, we can conclude the below points with async code :
通过与先前的同步代码输出进行比较,我们可以用异步代码得出以下几点结论:
- The above-executed codes show a non-blocking output 上面执行的代码显示了非阻塞输出
结论 (CONCLUSION)
We hope that this blog helped you in a good way. Still, confused with the process? Please Contact our Nodejs experts for more details regarding the development process.
我们希望该博客对您有所帮助。 仍然对过程感到困惑吗? 请联系我们的Nodejs专家以获取有关开发过程的更多详细信息。
翻译自: https://medium.com/@Haribabu_DM/blocking-and-non-blocking-operations-in-nodejs-c30d5208e50
node.js非阻塞io