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

javascript - Spawning child processes in an express server - Stack Overflow

programmeradmin4浏览0评论

In my express server there are some functions which I need to run as child processes because otherwise they'll tie up the server and other people won't be able to access it. They're already using the async module but they still tie up the server unless they're run as child processes.

One problem is passing the req and res parameters to them.

How can this be done?

In my express server there are some functions which I need to run as child processes because otherwise they'll tie up the server and other people won't be able to access it. They're already using the async module but they still tie up the server unless they're run as child processes.

One problem is passing the req and res parameters to them.

How can this be done?

Share Improve this question asked Jun 3, 2012 at 11:45 node ninjanode ninja 33k61 gold badges173 silver badges257 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

Using child_process.fork, you can send messages to child processes.

Edit: I incorrectly advised to pass req and res as message parameters to the child process. This is not possible, as all messages to and from child processes are converted to JSON. Instead, you could keep some kind of queue in your server. The below is only meant as an example, you may want something more robust:

child.js:

process.on('message', function(message) {
    // Process data

    process.send({id: message.id, data: 'some result'});
});

server.js:

var child_process = require('child_process');
var child = child_process.fork(__dirname + '/child.js');
var taskId = 0;
var tasks = {};

function addTask(data, callback) {
    var id = taskId++;

    child.send({id: id, data: data});

    tasks[id] = callback;
};

child.on('message', function(message) {
    // Look up the callback bound to this id and invoke it with the result
    tasks[message.id](message.data);
});

app.post('/foo', function(req, res) {
    addTask('some data', function(result) {
        res.send(result);
    });
});

It's a bit more involved, but it should work. You may quickly grow out of such a system, and may be better served by a proper queue.

发布评论

评论列表(0)

  1. 暂无评论