我在child_process的叉子上遇到了一些问题,我想这就像stdio一样。
在我分叉了一些js代码之后,子程序工作,请求流数据,然后接收数据,只通过数据传递给父程序。在日志"Count1503“停止之后,当我将日志添加到子目录中时。(我不确定在你的情况下也是1499年,但无论你尝试了多少次,数据都是完全相同的)
我只是做了下面的示例代码(非常可爱的test.js)
// test.js
var express = require('express'),
app = express(),
child_process = require('child_process');
app.listen(3030, function() {
console.log('service started');
});
var child = child_process.fork(__dirname + '/test_child.js', [], { silent: true });
child.on('exit', function(code, signal) {
console.log('exit\n', code, signal);
})
// test_child.js
var winston = require('winston'),
logger = new winston.Logger({
level: 'silly',
transports: [
new (winston.transports.File)({
name: 'mainLog',
filename: 'child_test.log',
level: 'silly'
}),
new (winston.transports.Console)({
colorize: true,
})
]
});
var interval = 2,
cnt = 1,
count = 1;
setInterval(function() {
logger.info('Count[%d]', count++);
}, interval)
setInterval(function() {
logger.info('CNT[%d]', cnt++);
}, interval)
我猜这是关于stdio相关缓冲区的原因是当我添加一些文本作为日志时,日志计数会变短。
请帮助那些知道node.js的人
提前谢谢。
发布于 2016-10-03 23:02:06
当您将silent: true
传递给child_process.fork()
时,在父级和子级之间使用管道。在父脚本中,您永远不会读取子流(特别是stdout),因此在某个时候,内部缓冲区正在填充,内置的背压机制导致进程停止接受来自子进程的输出。
将类似这样的内容添加到父脚本中,以保持流的流动,并且它应该按预期工作:
child.stdout.resume();
child.stderr.resume();
这一特性只会忽略输出。如果需要输出,应该添加'data'
事件处理程序或确保经常调用流上的.read()
。
https://stackoverflow.com/questions/39845566
复制相似问题