最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js spawns multiple processes - Stack Overflow

programmeradmin2浏览0评论

I got a little bit confused about my node.js application. As far as I thought, node.js runs in a single process. However if I start my application by invoking node app.jsand monitor it with htop, I can see 4 sub-processes running, where I'd expect only one to be.

app.js

var express = require('express'),
    routes = require('./routes'),

    objects = require('./objects'),

    http = require('http'),
    path = require('path'),

    pinLayout = objects.pinlayout,

    // utils
    util = require('util'),
    wiringPi = require('wiring-pi'),
    winston = require('winston'),
    async = require('async');


// Logger - winston
var log = new(winston.Logger)({
    transports: [
        new(winston.transports.Console)({
            colorize: true,
            timestamp: true
        }),
        new(winston.transports.File)({
            filename: './log/app.log'
        })
    ]
});

// WiringPi
wiringPi.setup('gpio');

var app = express();

// all environments
app.set('port', process.env.PORT || 3001);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());

app.use(require('less-middleware')({
    src: __dirname + '/public',
    force: true,
    press: true
}));

app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// 404 Page
app.use(function(req, res, next) {
    res.render('404.jade', {
        title: "404 - Page Not Found",
        showFullNav: false,
        status: 404,
        url: req.url
    });
});

// development only
if ('development' == app.get('env')) {
    app.use(express.errorHandler({
        dumpExceptions: true,
        showStack: true
    }));
}

I got a little bit confused about my node.js application. As far as I thought, node.js runs in a single process. However if I start my application by invoking node app.jsand monitor it with htop, I can see 4 sub-processes running, where I'd expect only one to be.

app.js

var express = require('express'),
    routes = require('./routes'),

    objects = require('./objects'),

    http = require('http'),
    path = require('path'),

    pinLayout = objects.pinlayout,

    // utils
    util = require('util'),
    wiringPi = require('wiring-pi'),
    winston = require('winston'),
    async = require('async');


// Logger - winston
var log = new(winston.Logger)({
    transports: [
        new(winston.transports.Console)({
            colorize: true,
            timestamp: true
        }),
        new(winston.transports.File)({
            filename: './log/app.log'
        })
    ]
});

// WiringPi
wiringPi.setup('gpio');

var app = express();

// all environments
app.set('port', process.env.PORT || 3001);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());

app.use(require('less-middleware')({
    src: __dirname + '/public',
    force: true,
    press: true
}));

app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// 404 Page
app.use(function(req, res, next) {
    res.render('404.jade', {
        title: "404 - Page Not Found",
        showFullNav: false,
        status: 404,
        url: req.url
    });
});

// development only
if ('development' == app.get('env')) {
    app.use(express.errorHandler({
        dumpExceptions: true,
        showStack: true
    }));
}
Share Improve this question edited Nov 10, 2013 at 14:55 Alx asked Nov 10, 2013 at 14:47 AlxAlx 6,2757 gold badges38 silver badges55 bronze badges 3
  • Could you show us the code? Which modules do you use? We need more information. – Paul Mougel Commented Nov 10, 2013 at 14:50
  • 1 the app itself that you run is a single process - but you can spawn additional processes from that app if needed. Paul is right - please share more details – ali haider Commented Nov 10, 2013 at 14:54
  • Hey guys, I just did and removed the routes only, besides that I have only some constants and functions declared, but during startup nothing actually happens more than starting the webserver and waiting for requests. – Alx Commented Nov 10, 2013 at 14:56
Add a ment  | 

1 Answer 1

Reset to default 9

Although your code runs in a single thread, node.js uses a thread pool (mostly) for file system operations. This is needed because there are no async APIs for fs.

For example, when you call file.readFile, you will go through Read(), which will call:

ASYNC_CALL(read, cb, fd, buf, len, pos);

read is the blocking unix read(2). This will run in a thread until it pletes. These are the threads you are seeing.

发布评论

评论列表(0)

  1. 暂无评论