回调的NodeJS机制 - 这线程处理的回调? [英] Nodejs callback mechanism - which thread handles the callback?

查看:185
本文介绍了回调的NodeJS机制 - 这线程处理的回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的NodeJS,想知道有关节点的单实例模型。
在一个简单的应用程序的NodeJS,当一些封锁操作是异步回调与处理,确实运行在的NodeJS主线程处理回调也?
如果该请求是得到一些数据从数据库中,并且有并发用户100秒,并且每个数据库操作花费几秒钟的时间,当回调最终焙烧(对于每个连接的),是主线程接受这些请求用于执行回调呢?如果是这样,怎么做规模的NodeJS,它是如何反应如此之快?

I'm new to nodeJS and was wondering about the single-instance model of Node. In a simple nodeJs application, when some blocking operation is asynchronously handled with callbacks, does the main thread running the nodeJs handle the callback as well?. If the request is to get some data off the database, and there are 100s of concurrent users, and each db operation takes couple of seconds, when the callback is finally fired (for each of the connections), is the main thread accepting these requests used for executing the callback as well? If so, how does nodeJs scale and how does it respond so fast?.

推荐答案

中的NodeJS每个实例在一个线程中运行。期。当你做一个异步调用,比方说,一个网络请求,它不会等着它,而不是在你的code或其他地方。它有一个贯穿事件循环。当响应准备就绪后,它会调用回调函数。

Each instance of nodejs runs in a single thread. Period. When you make an async call to, say, a network request, it doesn't wait around for it, not in your code or anywhere else. It has an event loop that runs through. When the response is ready, it invokes your callback.

这可不得了高性能,因为它不需要大量的线程和所有的内存开销,但它意味着你必须要小心不要做同步阻塞的东西。

This can be incredibly performant, because it doesn't need lots of threads and all the memory overhead, but it means you need to be careful not to do synchronous blocking stuff.

有是事件循环在<一个一个pretty体面的解释href=\"http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/\">http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/和原来的 jsconf $ P $由Ryan Dahl的psentation的 http://www.youtube.com/watch?v=ztspvPYybIY 是值得关注的。见过一个工程师获得了全场起立鼓掌的技术presentation?

There is a pretty decent explanation of the event loop at http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/ and the original jsconf presentation by Ryan Dahl http://www.youtube.com/watch?v=ztspvPYybIY is worth watching. Ever seen an engineer get a standing ovation for a technical presentation?

这篇关于回调的NodeJS机制 - 这线程处理的回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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