Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does node.js implement non-blocking I/O?

From here i have found that node.js implements non-blocking i/o model. But i don't understand how.

As javascript is single threaded. How can a single thread do i/o operations and simultaneously executing the further process.

like image 624
codeofnode Avatar asked Aug 20 '13 11:08

codeofnode


People also ask

How does node JS perform non-blocking?

Non-Blocking methods are executed asynchronously. Asynchronously means that the program may not necessarily execute line by line. The program calls the function and move to the next operation and does not wait for it to return.

How does node js handle IO operations in a way that they don't block code execution?

js runtime accommodates non-blocking code will help you write better, and faster, applications. In Node, I/O operations are offloaded to the C++ APIs (libUV) which allows the Node. js main thread to continue executing your code without having to wait for file or network operations to finish.

How node JS overcome the problem of blocking of IO operations?

Note: So, the solution to this problem in node. js is to use asynchronous non-blocking code and Node. js uses an event loop for this. “An object that handles and processes external events and converts them into call-back calls” is what an event loop is.

How node js single-threaded non-blocking IO works?

Working of single-threaded non-blocking IO:This thread reads the client request, processes the request, performs any blocking IO operations if needed, and prepares the final response to be sent back to the server. The event loop sends this response back to the respective client.


1 Answers

It is true that operations such as sleep will be blocking the thread. But I/O events can indeed be asynchronous.

Node.js uses an event loop for this. An event loop is “an entity that handles and processes external events and converts them into callback invocations”

Whenever data is needed nodejs registers a callback and sends the operation to this event loop. Whenever the data is available the callback is called.

http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/ for more info

like image 76
Hless Avatar answered Sep 27 '22 18:09

Hless