如何是天生的Node.js更快时,它仍然依赖于内部线程? [英] How is Node.js inherently faster when it still relies on Threads internally?

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

问题描述

我刚看了下面的视频:介绍的Node.js 仍不明白你怎么弄的速度好处。

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

主要是,在一个点瑞恩·达尔(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的开发人员不必为code螺纹细节,但它的下面仍然使用线程来处理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.

推荐答案

有实际上正在此间混为一谈了一些不同的东西。但它开始与米姆的线程只是真的很难。所以,如果他们是辛苦,你更容易,使用时线程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.

想想他给人的例子,其中一个请求到达约一,你运行一些查询,然后做的,结果什么。如果你在一个标准程序的方式写出来,在code可能是这样的:

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 );

如果收到的请求给您造成创建跑到上面code一个新的线程,你就会有一个线程坐在那里,什么都不做,而当查询()正在运行。 (Apache的,根据瑞安,是使用一个单独的线程来满足原来的要求,而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.)

现在,如果你真聪明,你会前preSS的code以上的方式在那里,当你运行该查询的环境可能会熄灭,做些别的事情:

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的是做。你基本上是装饰 - 的方式,是因为语言和环境,因此关于闭包点的方便 - 以这样的方式,环境可以巧妙你code运行什么,以及何时。这样一来,Node.js的不是的新的的在这个意义上,它发明了异步I / O(不是每个人声称这样的事),但它在该方式是新的前pressed是一个有点不同。

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更快时,它仍然依赖于内部线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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