我正在使用Node.js產生100個以上的子進程,可能甚至是1000.我關心的是父進程可能會成爲某種瓶頸如果所有的子進程的stdout/stderr必須通過父進程才能在某處登錄。從父進程派生/產生許多node.js進程的最高性能方式
所以我的假設是,爲了達到最高的性能/吞吐量,我們應在父進程忽略標準輸出/標準錯誤,就像這樣:
const cp = require('child_process');
items.forEach(function(exec){
const n = cp.spawn('node', [exec], {
stdio: ['ignore','ignore','ignore','ipc']
});
});
我的問題是,如何在性能損失的錢它以這種方式使用管道:
// (100+ items to iterate over)
items.forEach(function(exec){
const n = cp.spawn('node', [exec], {
stdio: ['ignore','pipe','pipe','ipc']
});
});
這樣,stdout和stderr被傳送到父進程?我假設的性能損失可能是激烈的,特別是如果我們處理標準輸出/標準錯誤在父進程中,像這樣:
// (100+ items to iterate over)
items.forEach(function(exec){
const n = cp.spawn('node', [exec], {
stdio: ['ignore','pipe','pipe','ipc']
});
n.stdout.setEncoding('utf8');
n.stderr.setEncoding('utf8');
n.stdout.on('data', function(d){
// do something with the data
});
n.stderr.on('data', function(d){
// do something with the data
});
});
我假設
- 我認爲,如果我們用「忽略」的標準輸出和在父進程中stderr, 這比管道標準輸出/標準錯誤到父進程更高性能。
我認爲,如果我們選擇一個文件流的標準輸出/標準錯誤喜歡這樣
stdio: ['ignore', fs.openSync('/some/file.log'), fs.openSync('/some/file.log'),'ipc']
,這幾乎是用「忽略」的標準輸出/標準錯誤(應該送作爲高性能stdout/stderr到/ dev/null)
這些假設是否正確?關於stdout/stderr,如果我想將stdout/stderr記錄到某處(而不是/ dev/null),如何實現最高性能?
注:這是一個庫,所以stdout/stderr的數量可能會有很大的不同。此外,最有可能很少分叉更多的進程比核心,最多同時運行約15個進程。
如果源代碼出現問題,部分答案在這裏:https://github.com/nodejs/node/blob/master/lib/child_process.js –
並在這裏:https://github.com/ nodejs/node/blob/master/lib/internal/child_process.js –
你的程序庫需要分叉多少子進程? – robertklep