7

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.

3 Answers 3

10

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

Sign up to request clarification or add additional context in comments.

1 Comment

Just about to post that link. It's a good article that should explain what you need to know.
9

The I/O that is handled by node.js is multithreaded internally.

It is the programming interface that is single threaded and asynchronous.

Comments

0

Ryan Dahl: Original Node.js presentation at JSConf 2009 (Ryan is the creator of Node.js)

This video will answer your question.

The essence of JavaScript(v8) is event callbacks (button onclick="functions()" etc). That's how the I/O is multithreaded. We still have to write our code to not have blocking I/O by only writing callbacks; otherwise the code will wait on db.query responses and be blocking before executing the next line of code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.