标签:node.js process net emit fork
详细代码可以看https://github.com/GitHubSi/中的app代码:
1. 多进程
root@ubuntu:/home/fuhui/Public/node# vi master.js
var fork = require('child_process').fork;
var cpus = require('os').cpus();
//for(var i = 0; i<cpus.length; i++){
for(var i = 0; i<2; i++){
fork('./worker.js');
// fork('./worker.js');
}
~
root@ubuntu:/home/fuhui/Public/node# vi worker.js
var http =require('http');
http.createServer(function(req,res){
res.writeHead(200, {'Content-Type':'text/plain'});
res.end('Hello World');
}).listen(Math.random*10000+1,'127.0.0.1');~
~
root@ubuntu:/home/fuhui/Public/node# vi parent.js
var cp = require('child_process');
var n = cp.fork(__dirname + '/sub.js');
n.on('message' , function(m){
console.log('PARENT got message:',m);
});
n.send({hello:'world'});
~
~
~
root@ubuntu:/home/fuhui/Public/node# vi sub.js
process.on('message',function(m){
console.log('CHILD got mesage',m);
});
process.send({foo:'bar'});
~
修改worker.js
root@ubuntu:/home/fuhui/Public/node# vi worker.js
var http =require('http');
var ser = http.createServer(function(req,res){
res.writeHead(200, {'Content-Type':'text/plain'});
res.end('Hello World');
});//.listen(Math.random*10000+1,'127.0.0.1');
ser.listen(8888,'127.0.0.1');root@ubuntu:/home/fuhui/Public/node# node master.js
events.js:85
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at exports._errnoException (util.js:746:11)
at Server._listen2 (net.js:1129:14)
at listen (net.js:1155:10)
at net.js:1253:9
at dns.js:85:18
at process._tickCallback (node.js:355:11)
at Function.Module.runMain (module.js:503:11)
at startup (node.js:129:16)
at node.js:814:3root@ubuntu:/home/fuhui/Public/node# vi master.js
var fork = require('child_process').fork;
var child = fork('worker.js');
var child1 = fork('worker.js');
var server = require('net').createServer();
server.listen(1337,function(){
child.send('server',server);
child1.send('server',server);
server.close();
});
//var cpus = require('os').cpus();
//for(var i = 0; i<cpus.length; i++){
//for(var i = 0; i<2; i++){
// fork('./worker.js');
// fork('./worker.js');
//}
~~
root@ubuntu:/home/fuhui/Public/node# vi worker.js
var http =require('http');
var ser = http.createServer(function(req,res){
res.writeHead(200, {'Content-Type':'text/plain'});
res.end('handle by child ,pid is ' +process.pid +'\n');
});//.listen(Math.random*10000+1,'127.0.0.1');
//ser.listen(8888,'127.0.0.1');
process.on('message',function(m,tcp){
if(m === 'server'){
tcp.on('connection',function(socket){
ser.emit('connection',socket);
});
}
});
~
~
~fuhui@ubuntu:~$ curl 127.0.0.1:1337 handle by child ,pid is 4337 fuhui@ubuntu:~$ curl 127.0.0.1:1337 handle by child ,pid is 4336 fuhui@ubuntu:~$ curl 127.0.0.1:1337 handle by child ,pid is 4337 fuhui@ubuntu:~$ curl 127.0.0.1:1337 handle by child ,pid is 4336 fuhui@ubuntu:~$ curl 127.0.0.1:1337 handle by child ,pid is 4337 fuhui@ubuntu:~$ curl 127.0.0.1:1337 handle by child ,pid is 4337 fuhui@ubuntu:~$ curl 127.0.0.1:1337 handle by child ,pid is 4336 fuhui@ubuntu:~$
标签:node.js process net emit fork
原文地址:http://blog.csdn.net/whynottrythis/article/details/46337041