Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why settimeout blocks eventloop

Tags:

node.js

Note: this is not a replicated post for those about settimeout, the key answer here is browser design options.

I am starting study node.js: A simple example to test async:

var http=require('http');

http.createServer(
    function(request, response){


        response.writeHead(200);
        response.write("Hello, dog is running");
        setTimeout(
            function(){
                response.write("Dog is done");
                response.end();
            },
            10000
        );

    }
).listen(8080);
console.log("Listen on port 8080") 

One interesting thing is its behavior is differernt when in command lind with curl and in browser: In Ubuntu 12.10, I use curl localhost:8080 in two consoles, they response in almost same 10 sends.

However, I open two browsers, make the request at almost same time, but the whole procedure took me 20 seconds?

thanks.

like image 287
Kuan Avatar asked Apr 06 '13 14:04

Kuan


1 Answers

It's the browser waiting, not node.js

If you run the server and request http://localhost:8080/ in two tabs it takes 20 seconds because the browser waits for the first request to the same url before starting the second.

If you run the server and request http://localhost:8080/1 and http://localhost:8080/2 in two tabs it takes 10 seconds again.

like image 174
generalhenry Avatar answered Nov 12 '22 14:11

generalhenry