Node.js 中的非阻塞或异步 I/O 是什么? [英] What is non-blocking or asynchronous I/O in Node.js?

查看:34
本文介绍了Node.js 中的非阻塞或异步 I/O 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务器端 Javascript 引擎的上下文中,什么是非阻塞 I/O 或异步 I/O?我认为这被认为是优于 Java 服务器端实现的优势.

In the context of Server Side Javascript engines, what is non-blocking I/O or asynchronous I/O? I see this being mentioned as an advantage over Java server side implementations.

推荐答案

同步 vs 异步

同步执行通常是指代码按顺序执行.异步执行是指不按照它在代码中出现的顺序运行的执行.在以下示例中,同步操作会导致警报按顺序触发.在异步操作中,虽然 alert(2) 似乎是第二次执行,但实际上并没有.

Synchronous vs Asynchronous

Synchronous execution usually refers to code executing in sequence. Asynchronous execution refers to execution that doesn't run in the sequence it appears in the code. In the following example, the synchronous operation causes the alerts to fire in sequence. In the async operation, while alert(2) appears to execute second, it doesn't.

同步:1,2,3

alert(1);
alert(2);
alert(3);

异步:1,3,2

alert(1);
setTimeout(() => alert(2), 0);
alert(3);

阻塞是指阻止进一步执行直到该操作完成的操作.非阻塞是指不阻塞执行的代码.在给定的示例中,localStorage 是一个阻塞操作,因为它会阻止执行读取.另一方面,fetch 是一个非阻塞操作,因为它不会阻止 alert(3) 执行.

Blocking refers to operations that block further execution until that operation finishes. Non-blocking refers to code that doesn't block execution. In the given example, localStorage is a blocking operation as it stalls execution to read. On the other hand, fetch is a non-blocking operation as it does not stall alert(3) from execution.

// Blocking: 1,... 2
alert(1);
var value = localStorage.getItem('foo');
alert(2);

// Non-blocking: 1, 3,... 2
alert(1);
fetch('example.com').then(() => alert(2));
alert(3);

优势

非阻塞、异步操作的一个优点是您可以最大限度地利用单个 CPU 和内存.

Advantages

One advantage of non-blocking, asynchronous operations is that you can maximize the usage of a single CPU as well as memory.

同步、阻塞操作的一个例子是一些 Web 服务器(如 Java 或 PHP 中的服务器)如​​何处理 IO 或网络请求.如果您的代码从文件或数据库中读取,则您的代码会阻止"执行之后的所有内容.在那段时间里,您的机器为一个没有做任何事情的线程占用内存和处理时间.

An example of synchronous, blocking operations is how some web servers like ones in Java or PHP handle IO or network requests. If your code reads from a file or the database, your code "blocks" everything after it from executing. In that period, your machine is holding onto memory and processing time for a thread that isn't doing anything.

为了满足其他请求而该线程已停止取决于您的软件.大多数服务器软件所做的是产生更多线程来满足额外的请求.这需要消耗更多内存和更多处理.

In order to cater other requests while that thread has stalled depends on your software. What most server software do is spawn more threads to cater the additional requests. This requires more memory consumed and more processing.

异步的、非阻塞的服务器——就像在 Node 中制作的——只使用一个线程来服务所有的请求.这意味着 Node 的实例可以充分利用单个线程.创作者设计的前提是I/O和网络操作是瓶颈.

Asynchronous, non-blocking servers - like ones made in Node - only use one thread to service all requests. This means an instance of Node makes the most out of a single thread. The creators designed it with the premise that the I/O and network operations are the bottleneck.

当请求到达服务器时,它们一次被处理一个.但是,例如,当服务的代码需要查询数据库时,它会将回调发送到第二个队列并且主线程将继续运行(它不会等待).现在,当 DB 操作完成并返回时,相应的回调会从第二个队列中拉出并在第三个队列中排队等待执行.当引擎有机会执行其他事情时(例如当执行堆栈被清空时),它会从第三个队列中获取回调并执行它.

When requests arrive at the server, they are serviced one at a time. However, when the code serviced needs to query the DB for example, it sends the callback to a second queue and the main thread will continue running (it doesn't wait). Now when the DB operation completes and returns, the corresponding callback pulled out of the second queue and queued in a third queue where they are pending execution. When the engine gets a chance to execute something else (like when the execution stack is emptied), it picks up a callback from the third queue and executes it.

这篇关于Node.js 中的非阻塞或异步 I/O 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆