Blocking vs non-blocking in node.js
The most common task that any developer would do is File I/O, this being a very fundamental process :
Open a File.
Read it's contents and print.
Do something else.
Let's see how this can be done with blocking and non-blocking code in node.js and see how it matter!
What we trying to do here, is reading tow simple files : hosts and users and printing there contents, meanwhile printing few hello msgs.
Contents of hosts file :
192.168.1.1
127.0.0.1
255.255.255.0
Contents of users file :
hemanth
lenord
monkey
cat
__Blocking or Synchronous code : __
var fs = require('fs'); var contents = fs.readFileSync('users','utf8'); console.log(contents); console.log("Hello Node\n"); var contents = fs.readFileSync('hosts','utf8'); console.log(contents); console.log("Hello again!");
Let's observer the output for a while, before we dig into blocking or Synchronous code :
hemanth
lenord
monkey
cat
Hello Node
192.168.1.1
127.0.0.1
255.255.255.0
Hello again!
Now relate the output to the code, we may notice the below major points :
It's a blocking code i.e until the read operation is completed, the next lines of code is not executed.
We see the contents of the user file, the 'Hello Node' strongly support the above point.
Same happens with the next file.
__Blocking or Asynchronous code : __
var fs = require('fs'); var contents = fs.readFile('./users','utf8', function(err,contents){ console.log(contents); }); console.log("Hello Node\n"); var contents = fs.readFile('./hosts','utf8', function(err,contents){ console.log(contents); }); console.log("Hello again!");
Let's observer the output again, before we dig into blocking or asynchronous code :
Hello Node
Hello again!
hemanth
lenord
monkey
cat
192.168.1.1
127.0.0.1
255.255.255.0
Comparing the previous output from sync code, we can conclude the below points with async code :
It's non-blocking as we can see from the output.
"Hello Node', "Hello again" and then the file contents are printed supporting the above point.
Let's try to understand more about asynchronous code :
Reading the file content looks something like :
var contents = fs.readFile('./users','utf8', function(err,contents){ console.log(contents); });
Looks, bit strange for one who is doing async code for the first time, but to make it simple, see the std def :
fs.readFile(filename, [encoding], [callback])
Compare the above with to sync call :
fs.readFileSync(filename, [encoding])
So we notice that [callback] is the extract param, and that is function(err,contents) in our case.
So what is callback?!
Callback is a reference to a piece of executable code that is passed as an argument to other code. In our case we are passing a function!
Thus, using a callback we are achieving asynchronous behavior i.e non-blocking, as in the code shall proceed further and not wait until the read is complete, later one when the read is completed it prints the contents.
Well, this was a simple example of reading a file, just imagine a web-service that is coded in node.js it would really matter that code reamins as asynchronous as possible or else you shall have angry users!
Recent blog posts
- watir-webdriver web inspector
- gem list to gemfile
- Packing ruby2.0 on debian.
- Made it into The Guinness Book!
- to_h in ruby 2.0
- Filter elements by pattern jQuery.
- Better HTML password fields for mobile ?
- Grayscale image when user offline
- nth-child CSS pseudo-class Christmas colors
- EventEmitter in nodejs