英文:
Timeout in createReadStream pipe
问题
I have an issue with Promise and an API rate limit.
我在使用Promise和API速率限制时遇到了问题。
I want to read a CSV file and call an API for each line of this CSV.
我想读取一个CSV文件,并为该CSV的每一行调用一个API。
Here is what I did:
这是我所做的:
fs.createReadStream("./input.csv")
.pipe(parse({ delimiter: ",", from_line: 1 }))
.on("data", async (row) => {
promises.push(processData(row, data));
await timeout();
})
.on("end", async () => {
await Promise.all(promises);
stringify(data, (err, output) => {
fs.writeFileSync("output.csv", output);
});
resolve();
})
.on("error", function (error) {
console.log(error.message);
});
});
with processData being:
processData函数如下:
async function processData(row, data) {
const firstName = row[0];
const infos = await callAPI(firstName);
await timeout();
const newRow = [
firstName,
infos
];
data.push(newRow);
return data;
}
and the timeout function:
timeout函数如下:
function timeout() {
return new Promise((resolve) =>
setTimeout(resolve, randomIntFromInterval(3000, 39990))
);
}
It doesn't work, all the promise start at the same time and the API is flooded. I tried to put the timeout() before the promises.push() but it doesn't work either…
这不起作用,所有的Promise都同时开始执行,API被淹没了。我尝试在promises.push()之前放置timeout(),但也不起作用...
What did I miss?
我漏掉了什么?
英文:
I have an issue with Promise and an API rate limit.
I want to read a CSV file and call an API for each line of this CSV.
Here is what I did:
fs.createReadStream("./input.csv")
.pipe(parse({ delimiter: ",", from_line: 1 }))
.on("data", async (row) => {
promises.push(processData(row, data));
await timeout();
})
.on("end", async () => {
await Promise.all(promises);
stringify(data, (err, output) => {
fs.writeFileSync("output.csv", output);
});
resolve();
})
.on("error", function (error) {
console.log(error.message);
});
});
with processData being:
async function processData(row, data) {
const firstName = row[0];
const infos = await callAPI(firstName);
await timeout();
const newRow = [
firstName,
infos
];
data.push(newRow);
return data;
}
and the timeout function:
function timeout() {
return new Promise((resolve) =>
setTimeout(resolve, randomIntFromInterval(3000, 39990))
);
}
It doesn't work, all the promise start at the same time and the API is flooded. I tried to put the timeout() before the promises.push() but it doesn't work either…
What did I miss?
答案1
得分: 0
好的,这是修复方法:
const readStream = fs.createReadStream('./input.csv');
const writeStream = fs.createWriteStream('output.csv');
const parser = parse({ delimiter: ',', from_line: 1 });
readStream.pipe(parser);
for await (const row of parser) {
promises.push(processData(row, data));
await timeout();
}
await Promise.all(promises);
stringify(data, (err, output) => {
writeStream.write(output);
console.log('OK');
});
console.log('end');
英文:
Ok, I found the answer thanks to @jabaa comment
Here is the fix:
const readStream = fs.createReadStream('./input.csv');
const writeStream = fs.createWriteStream('output.csv');
const parser = parse({ delimiter: ',', from_line: 1 });
readStream.pipe(parser);
for await (const row of parser) {
promises.push(processData(row, data));
await timeout();
}
await Promise.all(promises);
stringify(data, (err, output) => {
writeStream.write(output);
console.log('OK');
});
console.log('end');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论