Reputation: 341
I am going to create a set of web services that include very compute intensive code. The calculations can run from 1/2 a second to 20 seconds. The computations exist in C code.
I would probably not have more than 20 simultaneous web service requests of which only a couple would be the longer running calculations.
My understanding is that node.js runs in a single thread so I would have to write a node.js addon which would interface to my C code. The C code would then have to execute in its own thread. I would use the thread pools provided by node.js to run these computations asynchronously. I would run this on a multi-core machine to maximize performance.
Does the architecture that I described sound correct and is node.js suited to do this? Would Apache or IIS be better at an application like this?
Upvotes: 1
Views: 553
Reputation: 11052
You can make a native C program, and have node execute the binary, and callback upon completion. I think there are a couple methods, here is an example of child_process.exec(command, [options], callback)
copied from nodejitsu:
var childProcess = require('child_process'),
ls;
ls = childProcess.exec('ls -l', function (error, stdout, stderr) {
if (error) {
console.log(error.stack);
console.log('Error code: '+error.code);
console.log('Signal received: '+error.signal);
}
console.log('Child Process STDOUT: '+stdout);
console.log('Child Process STDERR: '+stderr);
});
ls.on('exit', function (code) {
console.log('Child process exited with exit code '+code);
});
edit
If you need to pass input to your process after starting it, use child_process.spawn
instead.
Node can handle apache's tasks well; I use express to host files and dynamic pages; my application logic is javascript code that is called by Express when particular HTTP requests are made to particular routes.
You might bog your machine down unless you cap simultaneous computations. I have no knowledge or comment on thread pools. caolan/async can help manage concurrent computations (and other control flow tasks.)
Upvotes: 1