最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nodejs setInterval to repeat a task every 30s - Stack Overflow

programmeradmin1浏览0评论

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 on setInterval 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
Add a ment  | 

4 Answers 4

Reset to default 2

it 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.

发布评论

评论列表(0)

  1. 暂无评论