简体   繁体   English

来自文件读取流的Node.js不定式流

[英]Nodejs infinitive stream from file read stream

I'm facing troubles trying to accomplish pretty banal task. 我在尝试完成平庸的任务时遇到了麻烦。 I need to create nodejs Readable stream from input txt file. 我需要从输入txt文件创建nodejs可读流。 I need to perform some transforming on this stream (create JSON object for each line). 我需要对此流执行一些转换(为每行创建JSON对象)。

Problem is that I want this stream to be infinitive: after last line is read, stream should just start from beginning. 问题是我希望此流是不定式的:在读取最后一行之后,流应从头开始。 My solution works bit I'm getting warning message: 我的解决方案有效,但我收到警告消息:

(node) warning: possible EventEmitter memory leak detected. (节点)警告:检测到可能的EventEmitter内存泄漏。 11 drain listeners added. 添加了11个侦听器。 Use emitter.setMaxListeners() to increase limit. 使用generator.setMaxListeners()增加限制。

I hoped to find simple solutions without reading and buffering file directly. 我希望找到简单的解决方案,而不必直接读取和缓冲文件。

//Transform stream object
var TransformStream = function () {
    Transform.call(this, {objectMode: true});
};
util.inherits(TransformStream, Transform);

TransformStream.prototype._transform = onTransform;
TransformStream.prototype._flush = onEnd;

var ts = new TransformStream();

var infinStream = function () {
    var r = fs.createReadStream(filePath);
    r.pipe(split(), {end: false})
        .pipe(ts, {end: false});
    r.once('end', function () {
        //console.log('\n\n\n\nRead file stream finished. Lines counted:\n\n\n\n' + detectionCounter);
        r.removeAllListeners();
        r.destroy();
        infinStream();
    });
    return r;
};
infinStream();

return ts;

From the comments: 从评论:

I need server that will be live for 24h/day, and will simulate device output all the time. 我需要每天24小时处于活动状态的服务器,并将始终模拟设备输出。

To do that a recursive function is a good idea. 为此,递归函数是一个好主意。 The approach you make is ok. 您采用的方法是可以的。 When you don't need different transforming tasks on your data, a stream is not really needed. 当您不需要数据上的其他转换任务时,实际上就不需要流。 Simple Events can do exactly what you want and they are easier to understand. 简单事件可以完全满足您的需求,并且更易于理解。

The error in your code is the point, where you put your listener. 代码中的错误就是放置侦听器的关键所在。 The listenster r.once is inside your recursive function. 监听器r.once在您的递归函数中。 You are defining r inside your function, so with every function call a new r is created. 您在函数内部定义了r,因此每次调用函数都会创建一个新的r。 Because of that r.once does not work like you are expecting it. 因此,r.once无法像您期望的那样工作。

What you can do: 你可以做什么:

  1. Make a recursive function which emitts an event 创建一个发出事件的递归函数
  2. use the data from your event outside 使用外部事件的数据

This is just a simple concept by using simple events, which are fireing the whole time the data from your file: 这只是使用简单事件的简单概念,这些事件会一直触发文件中的数据:

// Your recursive function
var simulateDeviceEvents = function(){
  fs.readFile('device.txt', function (err, data) {
    if (err) throw err;
    // Just emit the event here
    Emitter.emit('deviceEvent', data);
  });
  //If this happens to fast you could also call it with
  //a timeout.
  simulateDeviceEvents();
};

// Start the function
simulateDeviceEvents();
//IMPORTANT: The listener must be defined outside your function!
Emitter.on('deviceEvent', function(data){
   // Do something with your data here
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM