var cp = require(‘child_process’); var child1 = cp.fork(‘child.js’); var child2 = cp.fork(‘child.js’);
var server = require(‘net’).createServer(); server.on('connection’, function (socket) { socket.end(‘handled by parent\n’); }); server.listen(1337, function () { child1.send('server’, server); child2.send('server’, server); });
//子进程 child.jsprocess.on('message’, function (m, server) { if (m === ‘server’) { server.on('connection’, function (socket) { socket.end('handled by child, pid is ' + process.pid + ‘\n’); }); } });
每次访问时候,为什么总是其中一个进程执行,而不是3个进程都执行一遍,主进程明明把服务器传给了两个子进程啊