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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With