-1

What I don't understand is, nodejs is asynchronous so I run multiple tasks doesn't matter how long they take, because the code parser continue to the other tasks and when the long task is completed it will let me know with a callback function, so where is the blocking for the CPU intensive tasks? Even if a task it takes 10 seconds the other lines of code in Js will continue to execute and to start other tasks. As NodeJs use only 4 threads for heavy tasks I understand that all this threads will be busy so here is come in place the scenario of why to don't use heavy cpu tasks with nodejs, am I right?

var listener = readAsync("I/O heavy calculation", function(){
   console.log("I run after the I/O is done."); 
 })

//The parse will send the request and pass to the next line of code

console.log("I run before I/O request is done")

I expect the global declared console.log to run before the callback function.

  • I do not understand what you are asking. Please reformulate your question with more detail and maybe a concrete example. – zero298 Jun 05 '19 at 15:43
  • How intensive I/O block the thread in nodeJs and why? – android.replay Jun 05 '19 at 15:49
  • Please add the definition of `readAsync`. Depending on how it is implemented, it may block the thread. – zero298 Jun 05 '19 at 16:01
  • readAsync is a function which run the first parameter of the function readAsync(param1, callback), like setTimeout(), during the cpu processing of param1 the parser of js continue to read the code without stooping for the response of the param1 to be completed. – android.replay Jun 05 '19 at 16:09

1 Answers1

0

Nodejs programs are single threaded by design. It prioritizes the application of whats most important inside of the event loop. There are ways to optimize your performance at scale. For example, there is a cluster module built in node js to assign the same node clients to different workers.

https://nodejs.org/en/blog/release/v10.5.0/ from node 10.5 there is multi threading support but it is experimental.

Brad Ball
  • 599
  • 3
  • 5