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);
});
}
I found a
compute-clustermodule, which seems to be designed exactly for my goals. It spawns children processes, and the communication is made with IPC (no sockets wasted).