-
-
Save grantmichaels/432246 to your computer and use it in GitHub Desktop.
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 child_process = require('child_process'), | |
sys = require('sys'), | |
net = require('net'), | |
netBinding = process.binding('net'); | |
var fd = netBinding.socket('tcp4'); | |
netBinding.bind(fd, 8080); | |
netBinding.listen(fd, 128); | |
for (var i = 0; i < 4; i++) { | |
// Create an unnamed unix socket to pass the fd to the child. | |
var fds = netBinding.socketpair(); | |
// Spawn the child process | |
var child = child_process.spawn( | |
process.argv[0], | |
[__dirname + '/worker.js'], | |
undefined, | |
[fds[1], -1, -1] | |
); | |
// For some reason stdin isn't getting set, patch it externally | |
if (!child.stdin) { | |
child.stdin = new net.Stream(fds[0], 'unix'); | |
} | |
child.stdin.write(sys.inspect(fd), "ascii", fd); | |
// Echo output from child | |
child.stderr.addListener('data', function (data) { | |
process.stdout.write(data); | |
}); | |
} |
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 net = require('net'), | |
http = require('http'), | |
sys = require('sys'); | |
sys.debug("Child started " + process.pid); | |
// var stdin = process.openStdin(); | |
var stdin = new net.Stream(0, 'unix'); | |
stdin.addListener('fd', function(fd) { | |
http.createServer(function (req, res) { | |
res.writeHead(200, {"Content-Type": "text/plain"}); | |
res.end("Hello from " + process.pid); | |
}).listenFD(fd, "tcp4"); | |
}); | |
stdin.resume(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment