Ok, I'm learning Node.js but I couldn't wrap my mind around this waiting model. I'm learning it by reading The Node Beginner Book. In it there's a section about Blocking and Non-blocking operations. What I don't understand is the Non-blocking operations.
Here's the code:
var exec = require("child_process").exec;
function start(response) {
console.log("Request handler 'start' was called.");
exec("ls -lah", function (error, stdout, stderr) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(stdout);
response.end();
});
}
function upload(response) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello Upload");
response.end();
}
exports.start = start;
exports.upload = upload;
The start function called exec, and exec executes ls -lah. Then the callback will wait for a response right? What if exec executes "find /", in my computer it would take about 30 seconds to finish the "find /" command. Since this is single threaded, if User 1 access start function, then within milliseconds User 2 also access the start function too. Then what happens? Does it mean that User 1 will get the response in 30 seconds while User 2 will need to wait 1 minute because User 1 is still fulfilling the "find /"?
Sorry if my question is too noobish. Thanks for reading!
In node.js all I/O operations works asynchronously. Both find
operations will be run in parallel. Try read this: Understanding the node.js event loop.
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