Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Off-load heavy computations from main event loop in Node.js

Tags:

node.js

For some user requests, I need to do some heavy computations (taking about 100ms of time); and, of course, I do not want to perform these in node's main event loop so that it will prevent other requests from serving.

The most obvious, but definitely not the cleanest way to solve this problem would be to offload the computations to another program and wait for the results asynchronously.

Is there any way to do it without leaving the node process (and implementing inter-process communications)? E.g. something like this:

var compute = function (input) {
    var i,
        result = input;
    for (i = 0; i < 1000000; i++) {
        result = md5(result);
    }
}

var controller = function (req, res) {
    offload(compute, req.params.input, function(result) {
        res.send(result);
    });
}
like image 424
penartur Avatar asked May 14 '12 07:05

penartur


2 Answers

I found a compute-cluster module, which seems to be designed exactly for my goals. It spawns children processes, and the communication is made with IPC (no sockets wasted).

like image 69
penartur Avatar answered Nov 03 '22 15:11

penartur


The JavaScript language has no support for threads and node.js provides no interface to them either so you'll need to rely on a library to provide threading functionality. See the node-fibers and node-threads-a-go-go libraries for possible solutions.

If you're looking for a way to make IPC a little easier in node.js then consider using the zeromq library (node.js bindings). This somewhat-related-answer might be useful as well.

like image 36
maerics Avatar answered Nov 03 '22 14:11

maerics