node.js - Why does a while loop block the node event loop? -


the following example given in node.js book:

var open = false;  settimeout(function() {   open = true }, 1000)  while (!open) {   console.log('wait'); }  console.log('open sesame'); 

explaining why while loop blocks execution, author says:

node never execute timeout callback because event loop stuck on while loop started on line 7, never giving chance process timeout event!

however, author doesn't explain why happens in context of event loop or going on under hood.

can elaborate on this? why node stuck? , how 1 change above code, whilst retaining while control structure event loop not blocked , code behave 1 might reasonably expect; wait logged 1 second before settimeout fires , process exits after logging 'open sesame'.

generic explanations such answers to question io , event loops , callbacks not me rationalise this. i'm hoping answer directly references above code help.

it's simple really. internally, node.js consists of type of loop:

  • get event queue
  • run whatever task indicated , run until returns
  • when above task done, next item event queue
  • run whatever task indicated , run until returns
  • rinse, lather, repeat - on , over

if @ point, there nothing in event queue, go sleep until placed in event queue.


so, if piece of javascript sitting in while() loop, task not finishing , per above sequence, nothing new picked out of event queue until prior task done. so, long or forever running while() loop gums works. because javascript runs 1 task @ time (single threaded js execution), if 1 task spinning in while loop, nothing else can ever execute.

here's simple example might explain it:

 var done = false;   // set timer 1 second set done true  settimeout(function() {       done = true;  }, 1000);   // spin wait done value change  while (!done) { /* nothing */}   console.log("finally, done value changed!"); 

some might logically think while loop spin until timer fires , timer change value of done true , while loop finish , console.log() @ end execute. not happen. infinite loop , console.log() statement never executed.

the issue once go spin wait in while() loop, no other javascript can execute. so, timer wants change value of done variable cannot execute. thus, while loop condition can never change , infinite loop.

here's happens internally inside js engine:

  1. done variable initialized false
  2. settimeout() schedules timer event 1 second now
  3. the while loop starts spinning
  4. 1 second while loop spinning, timer fires internally js engine , timer callback added event queue. occurs on different thread, internal js engine.
  5. the while loop keeps spinning because done variable never changes. because continues spin, js engine never finishes thread of execution , never gets pull next item event queue.

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -