我正在对一个大文件创建文件读取流,并将其传递到readline.createInterface
。其目标是在此之后,使用for await ... of
从文件中获取行,而不将其全部读取到内存中。
但是,即使我什么也没读,整个流似乎仍在处理中。我知道发生这种情况是因为我试图监听事件(事件被发出),并且如果我使用很大的文件,我的脚本还需要一段时间才能完成。
是否有避免这种行为的方法?我希望按需消费流。
如果我不是使用readline
而是从流中读取大块并自己搜索行,则可以成功实现此行为,但是如果可能的话,我希望避免这种情况。
MWE:
var readline = require('readline');
var fs = require('fs');
var file = 'really_big_file.txt'; // 2GB is what I used
readline.createInterface({input: fs.createReadStream(file)});
// this takes a while to finish because the file is being read,// even if I'm not doing anything with the stream