javascript - Single Threaded Event Loop vs Multi Threaded Non Blocking Worker in Node.JS -
node.js biggest advantage it's non blocking nature. it's single threaded, doesn't need spawn new thread each new incoming connection.
behind event-loop (which in fact single threaded), there "non blocking worker". thing not single threaded anymore, (as far understood) can spawn new thread each task.
maybe misunderstood something, advantage. if there many tasks handle, wouldn't non blocking working turn blocking worker?
thanks christian
you need read libuv, "magic" behind node's non-blocking i/o.
the important thing take away libuv book libuv uses host os's asynchronous i/o facilities; it not create new thread every connection.
libuv tells os know changes (connection state, data received, etc) happen on particular set of sockets. os deal managing connections. os may create 1 or more threads accomplish that, that's not our concern. (and won't create thread every connection.)
for other types of operations calls c libraries may computationally expensive (ie crypto), libuv provides thread pool on operations may run. since thread pool, again don't have worry thread count growing without bound. when pool busy, operations queued.
so yes, javascript runs on single thread. yes, node (via libuv) spawns many threads in background work need not block javascript thread. however, thread count controlled, , i/o doesn't own node-allocated thread because that's handled os.
Comments
Post a Comment