0

I'm trying to understand the architecture of Event loop in Node.js. I came across side by side comparison between a server with setTimeout() and one with sleep() by while clause. setTimeout() was handled asynchronously but sleep() wasn't. http://www.atmarkit.co.jp/fcoding/articles/websocket/01/websocket01a.html (written in Japanese)

I understood this somewhat. But, I came up with a question, 'How can I find which blocks a process and which doesn't by reading source'. How do you determine it?

2
  • The source of a node.js program, or of node.js itself? Either way, any function about which you have questions, you should be able to find documentation that specified whether the function blocks or not (node.js documentation will likely not specify unless a function does block). Any specific functions in mind? Commented Feb 9, 2012 at 7:10
  • Assume all functions block unless they document or tell you that they are non blocking. For example, it's not obvious that console.log is non blocking and util.debug blocks. Commented Feb 9, 2012 at 18:33

1 Answer 1

1
  1. look at function signature: if it is var result = calculateData(parameters) than it block (btw every function in node block io processing loop, but most functions just queue io requests and exit)
  2. measure function execution time, 'non-blocking' function execution time should be small compared to time until result callback is called.
 var start = new Date();
 doesItBlock(function(err, result) {
     console.log('doesItBlock callback called after ' + (new Date - start));
 });
 console.log('doesItBlock exited after ' + (new Date - start));
Sign up to request clarification or add additional context in comments.

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.