admin管理员组文章数量:1023009
I built a nodejs application that should execute several tasks.
My app.js has a function call to the manager module which controls those tasks.
I want to call that function from my app.js and perform those tasks every 30s.
something like:
setInterval(manager.tasks(), 30000);
My question is that if using setInterval could give me any performance problems (slowing down the puter, blocking resources or any other reason)
Is there a more efficient way to do this or is setInterval ok?
I built a nodejs application that should execute several tasks.
My app.js has a function call to the manager module which controls those tasks.
I want to call that function from my app.js and perform those tasks every 30s.
something like:
setInterval(manager.tasks(), 30000);
My question is that if using setInterval could give me any performance problems (slowing down the puter, blocking resources or any other reason)
Is there a more efficient way to do this or is setInterval ok?
Share Improve this question asked Jun 19, 2019 at 8:54 EAzevedoEAzevedo 7892 gold badges18 silver badges47 bronze badges 3-
2
Uh, how heavy is your function? Because
setInterval
should have a negligable overhead by itself - it just queues stuff for later. If the task it executes takes 10s to plete, that might be a different thing but also doesn't really depend onsetInterval
that much. – VLAZ Commented Jun 19, 2019 at 8:56 -
2
You could also run the "job" which is triggered inside the
setInterval
from outside e.g. crontab. – Bernie Commented Jun 19, 2019 at 8:59 - I cant really say how heavy it is. The manager executes a few http requests and some other small things like filter arrays and so on... I thought about using the nodejs event module. When I am finish executing all the tasks I emit a signal and wait 30seconds before calling the manager again. Would that be a better solution? – EAzevedo Commented Jun 19, 2019 at 9:10
4 Answers
Reset to default 2it depends on how heavy the work/processing you want to do is, setInterval is async so your code will only be run once every 30 seconds, but at the same time JavaScript is single-threaded, so if you're doing a lot of work in your task, the one time it runs in 30 seconds it may take too long to execute thus blocking resources.
In most cases you should be fine using setInterval, but if you really want to emulate multi-threading or if you're doing too much work in your task, you may want to spawn another child process https://nodejs/api/child_process.html process or use the new Worker Threads API https://nodejs/api/worker_threads.html instead, but keep in mind its not as simple to implement as a setInterval call
Use node-cron or node-schedule instead
setInterval is implemented in standard node js, you won't have any performance / blocking problems, most libraries also use setInterval
It pletely depends on the function you executing inside setInterval. If it is I/O bound operation then you don't need to worry too much because libuv will handle itself But if it is CPU bound then I will suggest you to use child_process api to fork a new child process and do your stuff in to that.
I built a nodejs application that should execute several tasks.
My app.js has a function call to the manager module which controls those tasks.
I want to call that function from my app.js and perform those tasks every 30s.
something like:
setInterval(manager.tasks(), 30000);
My question is that if using setInterval could give me any performance problems (slowing down the puter, blocking resources or any other reason)
Is there a more efficient way to do this or is setInterval ok?
I built a nodejs application that should execute several tasks.
My app.js has a function call to the manager module which controls those tasks.
I want to call that function from my app.js and perform those tasks every 30s.
something like:
setInterval(manager.tasks(), 30000);
My question is that if using setInterval could give me any performance problems (slowing down the puter, blocking resources or any other reason)
Is there a more efficient way to do this or is setInterval ok?
Share Improve this question asked Jun 19, 2019 at 8:54 EAzevedoEAzevedo 7892 gold badges18 silver badges47 bronze badges 3-
2
Uh, how heavy is your function? Because
setInterval
should have a negligable overhead by itself - it just queues stuff for later. If the task it executes takes 10s to plete, that might be a different thing but also doesn't really depend onsetInterval
that much. – VLAZ Commented Jun 19, 2019 at 8:56 -
2
You could also run the "job" which is triggered inside the
setInterval
from outside e.g. crontab. – Bernie Commented Jun 19, 2019 at 8:59 - I cant really say how heavy it is. The manager executes a few http requests and some other small things like filter arrays and so on... I thought about using the nodejs event module. When I am finish executing all the tasks I emit a signal and wait 30seconds before calling the manager again. Would that be a better solution? – EAzevedo Commented Jun 19, 2019 at 9:10
4 Answers
Reset to default 2it depends on how heavy the work/processing you want to do is, setInterval is async so your code will only be run once every 30 seconds, but at the same time JavaScript is single-threaded, so if you're doing a lot of work in your task, the one time it runs in 30 seconds it may take too long to execute thus blocking resources.
In most cases you should be fine using setInterval, but if you really want to emulate multi-threading or if you're doing too much work in your task, you may want to spawn another child process https://nodejs/api/child_process.html process or use the new Worker Threads API https://nodejs/api/worker_threads.html instead, but keep in mind its not as simple to implement as a setInterval call
Use node-cron or node-schedule instead
setInterval is implemented in standard node js, you won't have any performance / blocking problems, most libraries also use setInterval
It pletely depends on the function you executing inside setInterval. If it is I/O bound operation then you don't need to worry too much because libuv will handle itself But if it is CPU bound then I will suggest you to use child_process api to fork a new child process and do your stuff in to that.
本文标签: javascriptNodejs setInterval to repeat a task every 30sStack Overflow
版权声明:本文标题:javascript - Nodejs setInterval to repeat a task every 30s - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745565615a2156430.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论