在同一个只读文件上创建多个可读流并同时以不同的光标位置从它们中读取是绝对安全的吗?
我只想确认一下。
假设我们有一个文件test.txt
,其中有0123456789\n
。
const FileSystem = require("fs");
function sleep(duration) {
return new Promise(resolve => setTimeout(resolve, duration));
}
async function ant_read(reader_name, readable) {
for await (const letter of readable) {
if(letter === "\n") {
break;
}
console.log(`${reader_name} "${letter}"`);
await sleep(Math.pow(Math.random() + 0.5, 5) * 1_000);
}
}
const s1 = FileSystem.createReadStream("test.txt", {encoding: "UTF-8", highWaterMark: 1});
const s2 = FileSystem.createReadStream("test.txt", {encoding: "UTF-8", highWaterMark: 1});
ant_read("Alpha", s1), ant_read("Beta", s2);
Alpha "0"
Beta "0"
Alpha "1"
Beta "1"
Alpha "2"
Beta "2"
Beta "3"
Beta "4"
Alpha "3"
Alpha "4"
Beta "5"
Alpha "5"
Alpha "6"
Alpha "7"
Alpha "8"
Beta "6"
Alpha "9"
Beta "7"
Beta "8"
Beta "9"