>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 reply to Re^4: The most precise second (timer) by misc
in thread The most precise second (timer) by tukusejssirs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.