当Node.js仍然依赖Threads在内部时,它本身是更快的? [英] How is Node.js inherently faster when it still relies on Threads internally?

查看:180
本文介绍了当Node.js仍然依赖Threads在内部时,它本身是更快的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚观看了以下视频: Node.js简介

I just watched the following video: Introduction to Node.js and still don't understand how you get the speed benefits.

主要是Ryan Dahl(Node.js的创建者)说Node。 js是基于事件循环的,而不是基于线程的。线程是昂贵的,并且应该仅由并发编程的专家来利用。

Mainly, at one point Ryan Dahl (Node.js' creator) says that Node.js is event-loop based instead of thread-based. Threads are expensive and should only be left to the experts of concurrent programming to be utilized.

然后,他展示了Node.js的架构堆栈,它有一个底层C实现,它在内部有自己的线程池。所以明显地Node.js开发人员永远不会启动自己的线程或直接使用线程池...他们使用异步回调。这很多我理解。

Later, he then shows the architecture stack of Node.js which has an underlying C implementation which has its own Thread pool internally. So obviously Node.js developers would never kick off their own threads or use the thread pool directly...they use async call-backs. That much I understand.

我不明白的是,Node.js仍然使用线程...它只是隐藏的实现,这是如何更快如果50个人请求50个文件(目前不在内存中)那么不需要50个线程?

What I don't understand is the point that Node.js still is using threads...it's just hiding the implementation so how is this faster if 50 people request 50 files (not currently in memory) well then aren't 50 threads required?

唯一的区别是,因为它在内部管理Node.js开发不必对线程细节进行编码,但是它仍然使用线程来处理IO(阻塞)文件请求。

The only difference being that since it's managed internally the Node.js developer doesn't have to code the threaded details but underneath it's still using the threads to process the IO (blocking) file requests.

所以你不是真的只是一个问题(线程)和隐藏它,而这个问题仍然存在:主要是多线程,上下文切换,死锁...等等?

So aren't you really just taking one problem (threading) and hiding it while that problem still exists: mainly multiple threads, context switching, dead-locks...etc?

这里还有一些细节我还是不明白。

There must be some detail I still do not understand here.

推荐答案

p>实际上有几个不同的东西在这里混在一起。但它从meme开始,线程只是真的很难。所以如果他们是硬的,你更有可能,当使用线程1)打破由于错误和2)不使用他们尽可能有效。 (2)是你要问的那个。

There are actually a few different things being conflated here. But it starts with the meme that threads are just really hard. So if they're hard, you are more likely, when using threads to 1) break due to bugs and 2) not use them as efficiently as possible. (2) is the one you're asking about.

想想他给出的一个例子,请求来了,你运行一些查询,然后做有什么结果的。如果你以标准的程序方式编写代码,代码可能如下所示:

Think about one of the examples he gives, where a request comes in and you run some query, and then do something with the results of that. If you write it in a standard procedural way, the code might look like this:

result = query( "select smurfs from some_mushroom" );
// twiddle fingers
go_do_something_with_result( result );

如果进来的请求导致您创建一个运行上述代码的新线程,有一个线程坐在那里,什么都不做,而 query()正在运行。 (Apache,根据Ryan,使用单个线程来满足原始请求,而nginx在他说的情况下超越了它)。

If the request coming in caused you to create a new thread that ran the above code, you'll have a thread sitting there, doing nothing at all while while query() is running. (Apache, according to Ryan, is using a single thread to satisfy the original request whereas nginx is outperforming it in the cases he's talking about because it's not.)

如果你真的聪明,你可以用一种方式表达上面的代码,在这种环境下,你可以在运行查询时执行其他操作:

Now, if you were really clever, you would express the code above in a way where the environment could go off and do something else while you're running the query:

query( statement: "select smurfs from some_mushroom", callback: go_do_something_with_result() );

这基本上是node.js在做什么。你基本上是装饰 - 方式是方便,因为语言和环境,因此关于关闭的点 - 你的代码这样的环境可以聪明的运行,什么时候。这样,node.js不是新的,因为它发明了异步I / O(不是任何人声称这样的东西),但它是新的,因为它的表达方式是一个不同。

This is basically what node.js is doing. You're basically decorating -- in a way that is convenient because of the language and environment, hence the points about closures -- your code in such a way that the environment can be clever about what runs, and when. In that way, node.js isn't new in the sense that it invented asynchronous I/O (not that anyone claimed anything like this), but it's new in that the way it's expressed is a little different.

注意:当我说环境可以聪明的运行和什么时候,具体我的意思是它用来启动一些I / O的线程现在可以用于处理某些其他请求,或者可以并行执行的某些计算,或者启动其他并行I / O。 (我不确定节点是复杂到足以开始更多的工作为同一个请求,但你得到的想法。)

Note: when I say that the environment can be clever about what runs and when, specifically what I mean is that the thread it used to start some I/O can now be used to handle some other request, or some computation that can be done in parallel, or start some other parallel I/O. (I'm not certain node is sophisticated enough to start more work for the same request, but you get the idea.)

这篇关于当Node.js仍然依赖Threads在内部时,它本身是更快的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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