了解NodeJS&非阻塞IO [英] Understanding NodeJS & Non-Blocking IO

查看:204
本文介绍了了解NodeJS&非阻塞IO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我最近注射了Node病毒,它正在编程世界中迅速传播。

So, I've recently been injected with the Node virus which is spreading in the Programming world very fast.

我对它的非阻塞IO很着迷方法并且确实已经尝试了几个程序。

I am fascinated by it's "Non-Blocking IO" approach and have indeed tried out a couple of programs myself.

然而,我目前无法理解某些概念。

However, I fail to understand certain concepts at the moment.

我需要外行术语的答案(来自Java背景的人)

I need answers in layman terms (someone coming from a Java background)

1。多线程&非阻塞IO。

让我们考虑实际情况。比如,我们有一个用户可以注册的网站。下面是代码。

Let's consider a practical scenario. Say, we have a website where users can register. Below would be the code.

..
..
   // Read HTTP Parameters
   // Do some Database work
   // Do some file work
   // Return a confirmation message
..
..

在传统的编程语言中,上述情况是按顺序进行的。并且,如果有多个注册请求,则Web服务器会创建一个新线程,其余的都是历史记录。当然,程序员可以创建自己的线程同时在第2行和第3行工作。

In a traditional programming language, the above happens in a sequential way. And, if there are multiple requests for registration, the web server creates a new thread and the rest is history. Of course, programmers can create threads of their own to work on Line 2 and Line 3 simultaneously.

在Node中,据我所知,第2行和第2行。当程序的其余部分执行并且解释器轮询第2行和第2行时,将并行运行3。每隔'x'毫秒3。

In Node, as I understand, Lines 2 & 3 will be run in parallel while the rest of the program gets executed and the Interpreter polls the lines 2 & 3 every 'x' ms.

现在,我的问题是,如果Node是单线程语言,那么第2行和第2行的工作是什么? 3正在执行程序的其余部分?

Now, my question is, if Node is a single threaded language, what does the job of lines 2 & 3 while the rest of the program is being executed?

2。可扩展性

我最近看到LinkedIn已经将Node作为其移动应用程序的后端进行了调整,并且已经有了很大的改进。

I recently read that LinkedIn have adapted Node as a back-end for their Mobile Apps and have seen massive improvements.

任何人都可以解释它是如何产生这样的差异的吗?

Can anyone explain how it has made such a difference?

3。适应其他编程语言

如果人们声称Node在性能方面有很大的不同,为什么没有其他编程语言改编了这种非阻塞IO范式?

If people are claiming that Node to be making a lot of difference when it comes to performance, why haven't other programming languages adapted this Non-Blocking IO paradigm?

我确定我错过了一些东西。只有你能解释我并指导我一些链接,才会有所帮助。

I'm sure I'm missing something. Only if you can explain me and guide me with some links, would be helpful.

谢谢。

推荐答案

提出了类似的问题,可能包含您要查找的所有信息:单线程非阻塞IO模型如何在Node.js中工作

A similar question was asked and probably contains all the info you're looking for: How the single threaded non blocking IO model works in Node.js

但我将简要介绍您的3个部分:

But I'll briefly cover your 3 parts:

1。

第2行和第3行以非常简单的形式可能看起来像:

       db.query(...,function(query_data){...});

       fs.readFile('/ path / to / file',function(file_data){...});


现在函数(query_data)和函数(file_data)是回调。 db.query和fs.readFile函数将发送实际的I / O请求,但回调允许处理来自数据库或文件的数据,直到收到响应为止。它并不真正轮询第2和第3行。回调被添加到事件循环中,并与其各自的I / O事件的某些文件描述符相关联。然后它轮询文件描述符以查看它们是否准备好执行I / O.如果是,它会使用I / O数据执行回调函数。

1.
Lines 2 and 3 in a very simple form could look like:
      db.query(..., function(query_data) { ... });
      fs.readFile('/path/to/file', function(file_data) { ... });

Now the function(query_data) and function(file_data) are callbacks. The functions db.query and fs.readFile will send the actual I/O requests but the callbacks allow the processing of the data from the database or the file to be delayed until the responses are received. It doesn't really "poll lines 2 and 3". The callbacks are added to an event loop and associated with some file descriptors for their respective I/O events. It then polls the file descriptors to see if they are ready to perform I/O. If they are, it executes the callback functions with the I/O data.

我认为除了代码之外,所有内容并行运行这一短语总结得很好。例如,Read HTTP parameters之类的内容将按顺序执行,但第2行和第3行中的I / O函数与添加到事件循环并稍后执行的回调相关联。所以基本上整点都是它不必等待I / O

I think the phrase "Everything runs in parallel except your code" sums it up well. For example, something like "Read HTTP parameters" would execute sequentially, but I/O functions like in lines 2 and 3 are associated with callbacks that are added to the event loop and execute later. So basically the whole point is it doesn't have to wait for I/O.

2。

由于1.中解释的内容,Node可以很好地扩展 I / O密集型请求,并允许许多用户同时连接。它是单线程的,因此它不一定适用于CPU密集型任务。

2.
Because of the things explained in 1., Node scales well for I/O intensive requests and allows many users to be connected simultaneously. It is single threaded, so it doesn't necessarily scale well for CPU intensive tasks.

3。

这paradigm已经与JavaScript一起使用,因为JavaScript支持回调,事件循环和闭包,这使得这很容易。在其他语言中并不一定如此。

3.
This paradigm has been used with JavaScript because JavaScript has support for callbacks, event loops and closures that make this easy. This isn't necessarily true in other languages.

我可能会稍微离开,但这是正在发生的事情的要点。

I might be a little off, but this is the gist of what's happening.

这篇关于了解NodeJS&非阻塞IO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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