rwaldron / temporal

Non-blocking, temporal task sequencing. For use with robots built with Johnny-Five
http://johnny-five.io/
MIT License
84 stars 12 forks source link

CPU usage at 100% ~all the time #12

Closed miro closed 9 years ago

miro commented 9 years ago

Not sure if I'm using this somehow wrong, but I'm getting continuous 100% CPU usage when looping stuff with temporal. See example code:

var pollIntervalInHours = 4;

temporal.loop((pollIntervalInHours * 60 * 60 * 1000), function() {
    console.log('# Temporal loop executing!');
    doStuff();
});

This issue is referenced in here also: https://github.com/rwaldron/johnny-five/issues/169#issuecomment-21324787, but upgrading to latest node (0.12.7) doesn't do any difference in my case.

rwaldron commented 9 years ago

This is the cost of using "recursive" setImmediate to create an extremely fast, async execution loop (it's not really recursive, since control is returned to the main process). It's this speed that allows temporal to review the internal schedule for tasks to execute more than once per millisecond, which is needed to create preferential execution cycles for hardware programming.

I won't beat around the bush, a program that wants to schedule something every 4 hours shouldn't use temporal at all—this library is for writing timing sensitive programs that are expected to be the primary process running on a given system, where the power source itself is tuned to accommodate that program specifically. Concrete examples include: walking robots (autonomous and remote control bipeds, quadrupeds or hexapods), driving robots (autonomous and remote control rovers), flying robots (autonomous and remote control single and multi-rotor helicopter), or water based robots (underwater rovs, surface boat-likes).

I should probably put all this in the readme

miro commented 9 years ago

Yeah, mentioning that on the README could be useful - I wouldn't be surprised if someone else would try to abuse this module the same way I did.

My use case was that I needed to do cron-like execution and I thought that I could make a shortcut with temporal and forever. If the CPU-usage wouldn't have been an issue, this approach would have saved me from setting required configuration environment variables at the cron config (forever gets user's env vars).

Thank you for your fast reply!

rwaldron commented 9 years ago

Couldn't you just use

setInterval(function() {
  //...

}, pollIntervalInHours * 60 * 60 * 1000);

(Maybe I'm missing something?)

miro commented 9 years ago

bedpq - imgur

Nope, you are not missing anything, I noticed that I am truly overengineering this instead of keeping it simple.

Your suggestion would work perfectly. Apologies for wasting your time.

rwaldron commented 9 years ago

No apologies necessary and certainly not a waste of my time—I'm glad that I could help you find a solution (even if it meant not using my module haha). Good luck with the rest of your project :)