英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论