That's the expected behavior when you have an infinite loop. Node has no
preemptive multithreading and will not interrupt such a loop.
On Saturday, August 10, 2013 4:41:33 PM UTC+3, ajlopez wrote:
>
> Hi!
>
> Bad English here ;-)
>
> Even with setImmediate, the output is the same.
>
> I guess the problem is the while(true)
>
> setImmediate(function nextTick1(){
> var a = 0;
> while(true){
> a++;
> //console.log("CURRENT : "+a);
> if(a%100000000==0){
> console.log("CURRENT PER LONGTIME : "+a);
> }
>
> }
> });
>
> Once nextTick1 TAKES CONTROL, the JavaScript thread in Node.js is
> dedicated to it, no timeout, no next tick or setImmediate enqueued function
> will be executed
>
> Angel "Java" Lopez
> @ajlopez
>
>
> On Sat, Aug 10, 2013 at 8:57 AM, chamsae <[email protected]
> <javascript:>>wrote:
>
>>
>> Hello, I am studying with "Professional Node.js by Pedro Teixeira" Korean
>> Edition.
>> In 55page, "Event Loop Blocking" section, I can't understand this code.
>>
>>
>> process.nextTick(function nextTick1(){
>> var a = 0;
>> while(true){
>> a++;
>> //console.log("CURRENT : "+a);
>> if(a%100000000==0){
>> console.log("CURRENT PER LONGTIME : "+a);
>> }
>>
>> }
>> });
>>
>> process.nextTick(function nextTick2(){
>> console.log("Next Tick");
>> });
>>
>>
>> setTimeout(function timeout(){
>> console.log("TIMEOUT");
>> }, 1000);
>>
>>
>> RESULT
>> ==================
>> CURRENT PER LONGTIME : 100000000
>> CURRENT PER LONGTIME : 200000000
>> CURRENT PER LONGTIME : 300000000
>> CURRENT PER LONGTIME : 400000000
>> CURRENT PER LONGTIME : 500000000
>> CURRENT PER LONGTIME : 600000000
>> CURRENT PER LONGTIME : 700000000
>> CURRENT PER LONGTIME : 800000000
>> CURRENT PER LONGTIME : 900000000
>> CURRENT PER LONGTIME : 1000000000
>> CURRENT PER LONGTIME : 1100000000
>> CURRENT PER LONGTIME : 1200000000
>> CURRENT PER LONGTIME : 1300000000
>> ...
>> ...
>> ...
>>
>> ==================
>>
>> nextTick2(), timeout() is never execute in this code's result.
>> only "while loop" would lasts.
>> I expected this,
>>
>> EXPECTED RESULT
>> ==================
>> CURRENT PER LONGTIME : 100000000
>> CURRENT PER LONGTIME : 200000000
>> CURRENT PER LONGTIME : 300000000
>> CURRENT PER LONGTIME : 400000000
>> CURRENT PER LONGTIME : 500000000
>> CURRENT PER LONGTIME : 600000000
>> CURRENT PER LONGTIME : 700000000
>> NextTick
>> CURRENT PER LONGTIME : 800000000
>> CURRENT PER LONGTIME : 900000000
>> CURRENT PER LONGTIME : 1000000000
>> CURRENT PER LONGTIME : 1100000000
>> TIMEOUT
>> CURRENT PER LONGTIME : 1200000000
>> CURRENT PER LONGTIME : 1300000000
>> ...
>> ...
>> ...
>>
>> ==================
>>
>> how can I change this code to get "EXPECTED RESULT"?
>>
>> and if I add STDIN code like this,
>>
>> ==================
>> process.stdin.resume();
>> process.stdin.setEncoding('utf8');
>>
>> process.stdin.on('data', function(chunk) {
>> process.stdout.write('data: ' + chunk);
>> });
>>
>> process.stdin.on('end', function() {
>> process.stdout.write('end');
>> });
>>
>>
>> process.nextTick(function nextTick1(){
>> var a = 0;
>> while(true){
>> a++;
>> //console.log("CURRENT : "+a);
>> if(a%100000000==0){
>> console.log("CURRENT PER LONGTIME : "+a);
>> }
>>
>> }
>> });
>>
>> process.nextTick(function nextTick2(){
>> console.log("Next Tick");
>> });
>>
>> setTimeout(function timeout(){
>> console.log("TIMEOUT");
>> }, 1000);
>>
>> ==================
>>
>> this code's result emitted just only console.log() in "while loop".
>> Is this I/O Blocking?
>> node.js is non-block I/O?
>>
>> I am confused this concept.
>> I'm sorry for asking you easiest question.
>>
>>
>>
>> --
>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to [email protected]<javascript:>
>> To unsubscribe from this group, send email to
>> [email protected] <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "nodejs" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.