搜索

首页  >  问答  >  正文

node.js - Node+express如何启用多线程?

我自己的写法是创建一个主进程master.js,带子进程worker.js,但是现在遇到一个问题就是,子进程遇到错误崩溃时会让主进程直接崩溃,原因是worker.js中的process.send({act: 'suicide'});行报错,错误代码:

TypeError: process.send is not a function

下方是源码:

//master.js

var express = require('express');
var port = process.env.PORT || 18080;
var app = express();

app.listen(port);

var fork = require('child_process').fork;
var cpus = require('os').cpus();

var workers = {};
var createWorker = function () {
    var worker = fork('./worker.js');
    // 退出时重新启动新的进程
    worker.on('exit', function () {
        console.log('Worker ' + worker.pid + ' exited.');
        delete workers[worker.pid];
        createWorker();
    });
    // 句柄转发
    worker.send('server', express);
    workers[worker.pid] = worker;
    console.log('Create worker. pid: ' + worker.pid);
};

for (var i = 0; i < cpus.length; i++) {
    createWorker();
}

// 进程自己退出时,让所有工作进程退出
process.on('exit', function () {
    for (var pid in workers) {
        workers[pid].kill();
    }
});
//worker.js

var worker;
process.on('message', function (m, tcp) {
    if (m === 'express') {
        worker = tcp;
        worker.on('connection', function (socket) {
            express.emit('connection', socket);
        });
    }
});

process.on('uncaughtException', function (err) {
    process.send({act: 'suicide'});
    // 停止接收新的连接
    worker.close(function () {
        // 所有已有连接断开后,退出进程
        process.exit(1);
    });
    // 5秒后退出进程
    setTimeout(function () {
        process.exit(1);
    }, 5000);
});

var createWorker = function () {
    var worker = fork(__dirname + '/worker.js');
    // 启动新的进程
    worker.on('message', function (message) {
        if (message.act === 'suicide') {
            createWorker();
        }
    });
    worker.on('exit', function () {
        console.log('Worker ' + worker.pid + ' exited.');
        delete workers[worker.pid];
    });
    worker.send('server', express);
    workers[worker.pid] = worker;
    console.log('Create worker. pid: ' + worker.pid);
};
PHP中文网PHP中文网2777 天前911

全部回复(3)我来回复

  • 迷茫

    迷茫2017-04-17 13:45:42

    我这边已经找到方法了,感谢borgchen指示,解决方法是:

    // app.js
    
    var cluster = require('cluster');
    var http = require('http');
    var numCPUs = require('os').cpus().length;
    
    if (cluster.isMaster) {
        // Fork workers
        for (var i = 0; i < numCPUs; i++) {
            cluster.fork();
        }
    
        cluster.on('exit', function(worker, code, signal) {
            console.log('worker ' + worker.process.pid + ' restart');
            setTimeout(function() {cluster.fork();},2000);
        });
    } else {
        // 这里放入口文件的东西并且直接使用express监听端口
        var express = require('express');
        var port = process.env.PORT || 18080;
        var app = express();
        app.listen(port);
    }

    这里不需要runmaster.jsworker.js,只需要runapp.js即可

    回复
    0
  • 高洛峰

    高洛峰2017-04-17 13:45:42

    多线程用的是cluster吧?

    回复
    0
  • 黄舟

    黄舟2017-04-17 13:45:42

    用pm2既可

    回复
    0
  • 取消回复