Last active
December 10, 2015 11:49
-
-
Save creationix/4430029 to your computer and use it in GitHub Desktop.
Example usage of runonce
This file contains hidden or 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
var runOnce = require('uvrun').runOnce; | |
// Do something here, like make a server to keep the event loop busy | |
var TCP = process.binding('tcp_wrap').TCP; | |
var server = new TCP(); | |
server.onconnection = function () { | |
console.log("connection!"); | |
}; | |
server.bind("0.0.0.0", 3000); | |
server.listen(511); | |
// Visualize each event loop tick using a custom event loop. | |
console.log("Waiting for events..."); | |
do { | |
var ret = runOnce(); | |
console.log("tick", Date.now()); | |
} while(ret); | |
// If the code gets here, there are no events left and node's built-in uv_run won't block. |
It's not parallel, it's just a new event loop that blocks inside the built-in one.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting way to achieve "parallel" execution. Is that any different from
process.nextTick
regarding performance?