简体   繁体   English

在 Node.js 中使用流写入大量数据

[英]Write huge amounts of data using stream in Node.js

Write a Node.js code to write a string "Hello world" 10^5 times in a file using stream writer in Node.js.编写 Node.js 代码,使用 Node.js 中的流编写器在文件中写入字符串“Hello world”10^5 次。

I have tried using loops but there is out of memory error and the system hangs.我曾尝试使用循环,但出现内存不足错误并且系统挂起。

Any better method?有什么更好的方法吗?

var fs = require('fs');
var wstream = fs.createWriteStream('D:\\myOutput.txt');
for(var i=0;i<10^5;i++){
wstream.write('Hello world!\n');
}
wstream.end();

Node.js is a single-threaded execution environment, when you write data in a loop the node.js will not get a chance to execute other tasks, as you can see from the documentation( https://nodejs.org/docs/latest-v12.x/api/stream.html#stream_writable_write_chunk_encoding_callback ) nodejs does not guarantee that all the data will be written once you call write, it can be buffered, if you want node to stay responsive, you have to free up the event loop ( https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#what-is-the-event-loop ) for other tasks, an example where you can do both is as follows. Node.js 是一个单线程的执行环境,当你在循环中写入数据时,node.js 将没有机会执行其他任务,正如你从文档中看到的( https://nodejs.org/docs/ latest-v12.x/​​api/stream.html#stream_writable_write_chunk_encoding_callback ) nodejs 不保证一旦你调用 write 就会写入所有的数据,它可以被缓冲,如果你想让 node 保持响应,你必须释放事件循环( https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#what-is-the-event-loop )用于其他任务,您可以同时执行的示例如下如下。 hope this clears things up希望这能解决问题

 var fs = require('fs'); var wstream = fs.createWriteStream('myOutput.txt'); wstream.on('error', function (err) { console.log(err); }); async function write() { for(var i=0;i<10^5;i++){ await writeStream(wstream, 'Hello world!\\n'); } wstream.end(); } async function writeStream (stream, data) { if (!stream.write(data)) { return new Promise((resolv, reject) => { stream.once('drain', () => {resolv();}); }); } else { return Promise.resolve(); } } write();

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

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