11

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?.

4

1 回答 1

10

nodejs 的每个实例都在单个线程中运行。时期。当您对网络请求进行异步调用时,它不会等待它,也不会在您的代码或其他任何地方等待。它有一个贯穿的事件循环。当响应准备好时,它会调用您的回调。

这可以是令人难以置信的性能,因为它不需要大量的线程和所有的内存开销,但这意味着你需要小心不要做同步阻塞的东西。

http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/和Ryan Dahl http :// /www.youtube.com/watch?v=ztspvPYybIY值得一看。见过工程师在技术演示中获得起立鼓掌吗?

于 2013-08-06T13:35:50.860 回答