“Timeout in createReadStream pipe” 翻译为中文是 “在 createReadStream 管道中超时”。

huangapple go评论61阅读模式
英文:

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');

huangapple
  • 本文由 发表于 2023年6月8日 23:58:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定