Home > OS >  node.js handling blocking IO operation
node.js handling blocking IO operation

Time:10-14

I want to understand internal working of node.js, I am intentionally including computation task ( for loop). But I see it is still blocking main thread.

Here is my script

console.log("start");

for (let i = 0; i < 10; i  ) {
    console.log(i)
}

console.log("end")

And the o/p is : start

1

2

3

....

10

end

But according to node.js architecture shouldn't high computation tasks be executed by different thread picked from thread pool and event loop continue executing non-blocking task? enter image description here

I am referencing node.js internal architecture using this link enter link description here

Can someone please explain the architecture and behavior of the script?

CodePudding user response:

By default, nodejs uses only ONE thread to run your Javascript with. That means that (unless you engage WorkerThreads which are essentially an entirely separate VM), only one piece of Javascript is ever running at once. Nodejs does not "detect" some long running piece of Javascript and move it to another thread. It has no features like that at all. If you have some long running piece of synchronous Javascript, it will block the event loop and block all other Javascript and all other event processing.

Internal to its implementation, nodejs has a thread pool that it uses for certain types of native code (internal implementations of file I/O and crypto operations). That only supports the implementation of asynchronous implementations for file I/O and crypto operations - it does not parallelize the running of Javascript.

So, your script you show:

console.log("start");

for (let i = 0; i < 10; i  ) {
    console.log(i)
}

console.log("end")

Is entirely synchronous and runs sequentially and blocks all other Javascript from running while it is running because it is using the one thread for running Javascript while it is running.

Nodejs gets its excellent scalability from its asynchronous I/O model that does not have to use a separate thread in order to have lots of asynchronous operations in flight at the same time. But, keep in mind that these asynchronous I/O operations all have native code behind them (some of which may use threads in their native code implementations).

But, if you have long running synchronous Javascript operations (like say something like image analysis written in Javascript), then those typically need to be moved out of the main event loop thread either by shunting them off to WorkerThreads or to other processes or to a native code implementation that may use OS threads.

But according to node.js architecture shouldn't high computation tasks be executed by different thread picked from thread pool and event loop continue executing non-blocking task?

No, that is not how nodejs works and is not a correct interpretation of the diagram you show. The thread pool is NOT used for running your Javascript. It is used for internal implementation of some APIs such as file I/O and some crypto operations. It is not used for running your Javascript. There is just one main thread for running your Javascript (unless you specifically run your code in a WorkerThread).

I want to understand internal working of node.js, I am intentionally including computation task ( for loop). But I see it is still blocking main thread.

Yes, a for loop (that does not contain an await statement that is awaiting a promise) will completely occupy the single Javascript thread and will block the event loop from processing other events while the for loop is running.

CodePudding user response:

JS executes its code Synchronouse. there are few things that gets "Asynchronouse" like setInterval or setTimout for exmple. But thats actually not fully true. Asynchronouse means things get done in parallel witch is not true. Take a look at setTimeout. By executing it you add the function into the task que, later the event loop grabs it from the que and put it onto the stack and executes it, syncrhonouse. If you want to execute something really parallel then you should consider using an worker thread

CodePudding user response:

There are absolutely no threads in JS (unless you explicitly use worker threads). Javascript uses cooperative multi-tasking which means that a function will always complete before the next one will start. The only other way to yield control back to the scheduler is to separate a task out into another function that is called asynchronously. So in your example, e.g., you could do:

console.log("start");
setTimeout(() => { 
  for (let i = 0; i < 10; i  ) {
    console.log(i)
  }}, 0);
console.log("end")

and you would get:

start
end
1
2
..
9

This also answers your question about heavy computations: unless you use the relatively new worker threads, you cannot run heavy computations in node.js "in the background" without the use of native code.

So if you really have heavy loads you have three options:

  • worker threads,
  • native code that is multi-threaded, e.g., written in C/C , or
  • breaking your computation down into small pieces, each one yielding control back to the scheduler when done (e.g., using map/reduce).
  • Related