`setInterval` 任务消耗的 CPU 太多了。

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

setInterval task is consuming WAY too much CPU

问题

I recently created a bot in node.js, but its super super heavy, uses 100% cpu! I use node-fetch, async.parallel and setInterval, im not sure why it uses 100% cpu, it might be maybe because setInterval queues? Idk. Here is my code though:

async.parallel({
  task1: function () {
    setInterval(function () {
      let itemRandomizer =
        config.generalInfo.itemList[
          Math.floor(Math.random() * config.generalInfo.itemList.length)
        ];
      let cookieRandomizer =
        randomCookies[Math.floor(Math.random() * randomCookies.length)];
      fetch(url, {
        method: "GET",
        agent,
        headers: {
          "Content-Type": "application/json",
          cookie: `cookie=${cookieRandomizer}`,
        },
      })
        .then((res) => res.json())
        .then((json) => {
          checks++;
          //console.log(checks, itemRandomizer, json.data[0].price)
        })
        .catch((err) => {});
    }, 0);
  },
  task2: function () {
    /* same code as task 1 (total 20 tasks) */
  },
});

I do not want to increase the interval, want it to stay as fast as it can, but 100% cpu is unreal, i dont want it to use that much, any work around? Is setInterval cpu-intensive?
Threads consume more CPU than async.parallel, so thats why i dont use threads.

英文:

I recently created a bot in node.js, but its super super heavy, uses 100% cpu! I use node-fetch, async.parallel and setInterval, im not sure why it uses 100% cpu, it might be maybe because setInterval queues? Idk. Here is my code though:

async.parallel({
  task1: function () {
    setInterval(function () {
      let itemRandomizer =
        config.generalInfo.itemList[
          Math.floor(Math.random() * config.generalInfo.itemList.length)
        ];
      let cookieRandomizer =
        randomCookies[Math.floor(Math.random() * randomCookies.length)];
      fetch(url, {
        method: "GET",
        agent,
        headers: {
          "Content-Type": "application/json",
          cookie: `cookie=${cookieRandomizer}`,
        },
      })
        .then((res) => res.json())
        .then((json) => {
          checks++;
          //console.log(checks, itemRandomizer, json.data[0].price)
        })
        .catch((err) => {});
    }, 0);
  },
  task2: function () {
    /* same code as task 1 (total 20 tasks) */
  },
});

I do not want to increase the interval, want it to stay as fast as it can, but 100% cpu is unreal, i dont want it to use that much, any work around? Is setInterval cpu-intensive?
Threads consume more CPU than async.parallel, so thats why i dont use threads.

答案1

得分: 2

你正在使用延迟为零的setInterval,因此你基本上在安排无限次运行任务。这很疯狂。你可能想要的是在一个任务完成后才开始一个新任务(或者也许开始几个,但在某个时候等待至少一个任务完成)。

顺便说一下,考虑使用类似p-map这样的工具,这样你可以轻松地将并发配置为像20这样的值,而不是试图基本上重新实现p-map已经做的事情。

英文:

You're using setInterval with a delay of zero, therefore you are scheduling basically infinite tasks to run. That's crazy. What you probably want is to start a new task only after one task is done (or maybe start a few, but at some point wait until at least one task is completed).

By the way, consider using something like p-map so that you can easily configure concurrency to a value like 20 instead of trying to basically re-implement what p-map already does.

huangapple
  • 本文由 发表于 2023年4月11日 11:29:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982196.html
匿名

发表评论

匿名网友

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

确定