0

nodejs runs on single thread, and when I read a file asynchronously then nodejs will run a thread in background to read that file, I believe the idea is to use as many CPU cores as possible.

Consider following snippet,

fs.readdirSync('my/path', (err, files) => {
  if (err) throw err;
  for(let i=0; i<files.length; i++){
    fs.readFile(files[i], (err, data) => {
      if (err) throw err;
      console.log(data);
    });
  }
});

Now, assuming the no. of files in my/path is 10. Now since nodejs uses one thread delicately for the eventloop, it is left with 7 CPU cores. How will nodejs handle this ? Is there any internal queue within nodejs or is it purely operating system implementation ?

laxman
  • 1,781
  • 4
  • 14
  • 32
  • 2
    Generally with modern I/O reading from a disk consumes zero or almost zero CPU time. You're unlikely to need even 2% of any CPU core to handle I/O. This is because I/O is managed outside of the CPU if possible by the DMA controller. The threads themselves are OS-level thread but not primarily for performance, it's just writing pure non-blocking disk I/O multi-platform is a nightmare. Now, networking is a different story because the API's evolved to be more compatible. For networking node does everything in one thread regardless of the number of network connections/cards you have – slebetman Jan 20 '20 at 10:40
  • 2
    ... with the exception of DNS requests of course. DNS are still mostly blocking on most OSes so node uses threads to handle DNS – slebetman Jan 20 '20 at 10:40
  • 1
    I've written lots of answers on SO regarding how asynchronous I/O works at various levels of abstraction: High-level software overview: https://stackoverflow.com/questions/34855352/how-in-general-does-node-js-handle-10-000-concurrent-requests/34857298#answer-34857298 – slebetman Jan 20 '20 at 10:49
  • 1
    How async I/O works down the stack from the app to the OS way down to the hardware: https://stackoverflow.com/questions/28961698/performance-of-nodejs-with-large-amount-of-callbacks/56713809#56713809 – slebetman Jan 20 '20 at 10:51
  • 1
    Design pattern around async I/O: https://stackoverflow.com/questions/56739934/is-nodejs-representing-reactor-or-proactor-design-pattern/56749862#56749862 – slebetman Jan 20 '20 at 10:51
  • 1
    Low-level software discussion (up to C level): https://stackoverflow.com/questions/29883525/i-know-that-callback-function-runs-asynchronously-but-why/29885509#29885509 – slebetman Jan 20 '20 at 10:52
  • @slebetman thanks for the links above, it did refreshed a lot of stuff for me. – laxman Jan 20 '20 at 12:37
  • Along with this, will a thread be always assigned a new core, if available or if OS uses some round robin/scheduling technique even if there are cores available ? I know this is more of a OS stuff but answer to this will be highly helpful. – laxman Jan 20 '20 at 12:41
  • 1
    That's an OS feature called CPU affinity (see https://en.wikipedia.org/wiki/Processor_affinity). Some OS allow more control than others. On most OSes the affinity is just a hint that tells the OS how you want your process to be handled but ultimately it's the OS that decides. – slebetman Jan 20 '20 at 13:45

1 Answers1

0

Node js's single process will always use only one core to execute it's task, for asynchronous behaviour ( the code which u mentioned) and non-blocking I/O event loop will make use of internal kernel level threads to work, but as single node process only one CPU core will be active.

For making use of multi cores of your machine, you can,

use cluster module to create child servers( processes) and assign each core to it else you can have multiple nodejs processes which can increase with no of cores.