您可以製作一個本地C程序,並讓節點執行二進制文件,並在完成時執行回調。我覺得有幾個方法,下面是nodejitsu複製的child_process.exec(command, [options], callback)
一個例子:
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);
});
編輯
如果需要啓動它後,通過輸入到你的過程中,使用child_process.spawn
代替。
節點可以很好地處理Apache的任務;我使用express來託管文件和動態頁面;我的應用程序邏輯是當特定的HTTP請求被做成特定路由時由Express調用的JavaScript代碼。
除非限制同時計算,否則可能會使機器停止工作。我對線程池沒有任何知識或評論。 caolan/async可以幫助管理併發計算(和其他控制流任務)。
這似乎只適用於獨立的可執行文件?我真的需要能夠調用一個函數並傳入並獲取複雜的數據。有沒有辦法做到這一點? – user265445 2013-05-01 09:37:47
我認爲這是可能的,我沒有任何進一步的知識 - gl! – Plato 2013-05-01 16:40:35