Question

Explain why it would be useful to use non-blocking I/O in a program. Give an example...

Explain why it would be useful to use non-blocking I/O in a program. Give an example of a situation where it could be used effectively. Why are signals important for non-blocking I/O?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Using non-blocking I/O in the right situation will improve throughput, latency, and/or responsiveness of your application. It also allows you to work with a single thread, potentially getting rid of synchronization between threads and all the problems associated with it. Node.js is single-threaded, yet can handle millions of connections with a couple of GB RAM without problems.

A common misconception lies in the fact that non-blocking I/O means fast I/O. Just because your I/O is not blocking your thread it does not get executed faster.

Here’s some example code from nodemailer, a library for sending email via node.js

var nodemailer = require('nodemailer');

nodemailer.SMTP = {
    host: 'smtp.example.com'
}

nodemailer.send_mail(
    {
      sender: 'me@example.com',
      to:'you@example.com',
      subject:'Hello!',
      html: '<p><b>Hi,</b> how are you doing?</p>',
      body:'Hi, how are you doing?'
    },
    function(error, success){
      console.log('Message ' + success ? 'sent' : 'failed');
    }
);

In this case sending mail is non-blocking. The results of the function are passed to the callback function which acts accordingly. The callback can call another function, throw an error, or write to a log. But crucially this doesn’t block the execution of other parts of the application. This is really good as by design we are saying nothing is allowed to block so we don’t need Redis, background processes and tools to monitor the background processes. Furthermore by using this philosophy we can create complex networked applications without needing to worry about one node in the network slowing the application to a halt.

Add a comment
Know the answer?
Add Answer to:
Explain why it would be useful to use non-blocking I/O in a program. Give an example...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT