The answer happens to be a combination of those given by Vadim and zer02, so I am leaving a write-up here. The real question is: can it be done better, either with setInterval() or other timers within node.js? Thanks in advance. But this scheme is going to be a bit complex, since calls may overlap in interesting ways also it will not be regular although it may look like it. For the 1000 ms call: check how many times the 20ms function has been called (e.g. For 1 ms calls: call them 20 times in the 20 ms callback. An example: set up 20 ms and 1000 ms setInterval() calls. Then use the long cycle as a "broom wagon" that makes any calls missed in the lower intervals. I don't know why a thousand calls are stressing my system so much, though, since node.js handles many more clients perfectly it must be true that setInterval() is CPU intensive (or I am doing something wrong).Īn obvious solution is to group function calls using longer timers, and then run short cycle function calls many times to simulate a shorter timer. Load average is high though, at about 0.50. Short cycle function calls are not overlapping since they just update a variable, and the node.js process is near 5% CPU even with a short cycle of 1 ms. I thought that the long cycles might be preventing the short ones, but it doesn't make any sense in the 1 ms case. Now for the dreaded question: what is going on here? Just joking obviously function calls are being skipped. There is this much cited Ressig post and lots of related JavaScript questions, but most assume that the code runs in a browser and not in node.js. I have found no references for this particular problem. The numbers vary, but are surprisingly consistent between runs: Running the first 1 ms benchmark three times yielded a delay after 10 seconds of 9267, 92. In this case it skips very few calls (the gap increased to 2 after 33 seconds and to 3 after 108 seconds. T23:02:05.364Z Seconds: 10, counter: 986, missed 14īetter, but roughly one function call is skipped every second. Here is how it behaves when the short timer is 1 ms: T23:03:32.780Z Seconds: 1, counter: 869, missed 131 Every second we print the difference between the number of expected ticks in the short cycle and the actual number of times the short counter was updated. There is a long timer of one second and a short one that can be adjusted using the variable short, in this case 1 ms. ![]() Log('Seconds: ' + seconds + ', counter: ' + ![]() The benchmark I used is as follows: var counter = 0 ![]() So I thought about using setInterval(), with funny results: many function calls were being skipped. Specifically some tasks every millisecond, others every 20 ms (50 times per second) and still others every second. For an upcoming project with node.js I need to perform various housekeeping tasks at periodic times.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |