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

查看:203
本文介绍了什么是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.

推荐答案

同步执行,通常是指code顺序执行。异步执行是指不会出现在code的顺序执行运行。在下面的例子中,同步操作导致警报顺序射击。在异步操作,而(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.

// Synchronous: 1,2,3
alert(1);
alert(2);
alert(3);

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

禁止VS非阻塞

马赛克是指阻止进一步的执行,直到操作完成操作。非阻塞是指code,它不会阻止执行。在给定的例子,的localStorage 是阻塞操作,因为它的执行档读取。在另一方面,是因为它不从执行搪塞警报(3)非阻塞操作。

Blocking vs Non-blocking

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或网络请求。如果你的code从文件或数据库,你的code块后,从执行的一切读取。在此期间,你的机器持有到内存和处理时间为一个线程的就是没有做任何事情的。

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.

异步非阻塞像节点发,只使用一个线程来服务所有的请求的人那些服务器。这意味着节点的实例,使最出一个单独的线程的。创作者以premise的I / O和网络操作的瓶颈而设计的。

Asynchronous, non-blocking servers like ones 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.

当请求到达服务器,它们被服务的一个在一个时间。然而,当code服务需要查询数据库为例,它发送回调到第二队列的和主线程将继续执行的(不等待)。现在,当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天全站免登陆