NodeJS 真的是单线程的吗?
Node.js 通过将基于事件的模型置于其核心,使用事件循环而不是线程来解决“每个连接一个线程问题”。 所有昂贵的 I/O 操作始终通过回调异步执行,该回调在启动的操作完成时执行。
如果发生任何操作,则观察是通过诸如 epoll() 之类的多路复用机制来处理的。
我现在的问题是:
为什么 NodeJS 在使用阻塞系统调用时不阻塞 选择/epoll/kqueue?
或者 NodeJS 根本不是单线程的,所以第二个线程是
是否需要使用 select/epoll/kqueue 观察所有 I/O 操作?
Node.js solves "One Thread per Connection Problem" by putting the event-based model at its core, using an event loop instead of threads.
All the expensive I/O operations are always executed asynchronously with a callback that gets executed when the initiated operation completes.
The Observation IF any Operation occurs is handled by multiplexing mechanisms like epoll()
.
My question is now:
Why doesn't NodeJS block while using the blocking Systemcalls
select/epoll/kqueue?Or isn't NodeJS single threaded at all, so that a second Thread is
necessary to observe all the I/O-Operations with select/epoll/kqueue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NodeJS 是事件(来自网站的第二行),而不是单线程。它在内部处理 select/epoll/kqueue 处理所需的线程,而无需用户明确管理它,但这并不意味着其中没有线程使用。
NodeJS is evented (2nd line from the website), not single-threaded. It internally handles threading needed to do select/epoll/kqueue handling without the user explicitly having to manage that, but that doesn't mean there is no thread usage within it.
不会。
当 I/O 操作启动时,它们会被委托给 libuv,后者使用自己的(多线程、异步)环境来管理请求。 libuv 宣布 I/O 操作完成,允许等待此事件的任何回调重新引入到主 V8 线程执行。
V8->委托 I/O (libuv) ->线程池->多线程异步
No.
When I/O operations are initiated they are delegated to libuv, which manages the request using its own (multi-threaded, asynchronous) environment. libuv announces the completion of I/O operations, allowing any callbacks waiting on this event to be re-introduced to the main V8 thread for execution.
V8 -> Delegate I/O (libuv) -> Thread pool -> Multi threaded async
JavaScript 是单线程的,事件模型也是单线程的。但Node堆栈不是单线程的。
Node使用V8引擎来实现并发。
JavaScript is single threaded, so is event-model. But Node stack is not single-threaded.
Node utilizes V8 engine for concurrency.
整体上没有 Nodejs 不是单线程的,但是 Node-Event 循环(nodeJS 大量使用的)是单线程的
一些 Node 框架/Std Lib 不是单线程的
No Nodejs in the whole is not single-threaded, but Node-Event loop (which nodeJS heavily uses) is single-threaded
Some of the node framework/Std Lib are not single-threaded