XP is just a number | |
PerlMonks |
Re^4: The most precise second (timer)by misc (Friar) |
on Dec 03, 2019 at 01:15 UTC ( [id://11109574]=note: print w/replies, xml ) | Need Help?? |
>For Perl scripts, we use the Highlander function to eliminate the forks. This, however, could effectively kill the processes we want to run each second (n seconds), when one instance is running longer that the period. >And generally, we need n * second intervals. Some scripts run each second, others every 5 seconds. I have created this question to find a way to run the scripts every n seconds most accurately. ..you could also use the pattern of a threadpool. Which I'd also like to suggest; instead of forking a new processes (if you do that), use something like Thread::Pool The other point, as you experienced, when you wait for 1 second, this will never be exactly one second. Sometimes another process gets scheduled first, sometimes not, memory is sometimes in the l1 cache, sometimes l3, and so on... The trick for getting more accuracy is to check the timer within a loop, and proceed, when the second ( or whatever interval is needed) has ticked. The loop is called "spinloop", since it just spins, until the condition ( time ) changed. This is also not 100% accurate. But the interval of e.g. 1 second will stay steady. Sometimes a few microseconds earlier, sometimes later. Although, as others also wrote, aiming at an exact microsecond timing is futile without rtos. And even then you might have to go the route, I described. There WILL be deviation, and this will cumulate. (When this is the problem). Spawning means forking, or also spawning threads - what I'd suggest. Utilizing a threadpool, you can also limit the number of threads, so the fork bombing problem is gone as well.
In Section
Seekers of Perl Wisdom
|
|