TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How does single-threaded node performs better than multi-threaded apache/nginx?

2 点作者 rms_returns将近 9 年前
I need some clarity on understanding Node.JS. To the best of my knowledge, my entire server.js app works on an event-based, non-blocking but single-threaded model. Unlike apache, it doesn&#x27;t spawn a new thread for each new request, right?<p>But if you consider the below code:<p>setTimeout(function () { for (var i = 0; i &lt; 1000000000; i++){} }, 1000);<p>Imagine that the above code is inside my server script. I&#x27;ve placed a 1 second timeout which is non blocking, but after that I&#x27;ve placed a great loop that blocks the entire thread for a long time.<p>In between that loop, if a new request comes, then what will happen? Node.JS will create a new instance of my server, right? So, Node.JS itself can&#x27;t be single threaded because its already busy with my loop. So overall, how many threads is my entire app (including the V8 engine) consuming?

2 条评论

mdomans将近 9 年前
Node.js internally has a small pool of threads. Processing of response&#x2F;request is by design handled in one thread designated to handle event loop.<p>Therefore - if your handler blocks, the _handling_ of requests will be blocked. Your server will still be able to ingest new requests.<p>The downside of this is that requests are stored internally in a queue so if your handler blocks for a long time, you can bring the whole node down.<p>Just in case you want a longer read on this: <a href="https:&#x2F;&#x2F;www.toptal.com&#x2F;nodejs&#x2F;top-10-common-nodejs-developer-mistakes" rel="nofollow">https:&#x2F;&#x2F;www.toptal.com&#x2F;nodejs&#x2F;top-10-common-nodejs-developer...</a><p>Or if you want to go really bare internals: <a href="http:&#x2F;&#x2F;www.journaldev.com&#x2F;7462&#x2F;node-js-processing-model-single-threaded-model-with-event-loop-architecture" rel="nofollow">http:&#x2F;&#x2F;www.journaldev.com&#x2F;7462&#x2F;node-js-processing-model-sing...</a>
vectorEQ将近 9 年前
This might be interesting for you. Sorry not Node expert myself, but it seems to touch some of your question. <a href="http:&#x2F;&#x2F;blog.mixu.net&#x2F;2011&#x2F;02&#x2F;01&#x2F;understanding-the-node-js-event-loop&#x2F;" rel="nofollow">http:&#x2F;&#x2F;blog.mixu.net&#x2F;2011&#x2F;02&#x2F;01&#x2F;understanding-the-node-js-ev...</a>