Created
June 19, 2014 19:46
-
-
Save rodrigosetti/18fafb78e2e0719d37dc to your computer and use it in GitHub Desktop.
Simple demonstration with node on the limitations of being single threaded
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Run this file with node, and point your browser to localhost:3000, | |
* you'll get a simple "Hello world." back immediately. | |
* | |
* If you go to localhost:3000/wait though, it will wait for 10 seconds | |
* before getting the "Hello world." - this is a CPU block, not an I/O. | |
* | |
* That means it blocks every other request to the server as well (you can | |
* test by going to localhost:3000 in another browser tab while you wait | |
* for the localhost:3000/wait). | |
*/ | |
var http = require('http'); | |
function cpuJob(duration) { | |
var startTime = new Date().getTime(); | |
while (new Date().getTime() - startTime < duration) { | |
// do nothing... | |
} | |
} | |
var server = http.createServer(function (req, res) { | |
if (req.url === '/wait') { | |
cpuJob(10000); | |
} | |
res.end("Hello world."); | |
}); | |
server.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment