|
NAMEIPC::Run::Timer -- Timer channels for IPC::Run. SYNOPSISuse IPC::Run qw( run timer timeout ); ## or IPC::Run::Timer ( timer timeout ); ## or IPC::Run::Timer ( :all ); ## A non-fatal timer: $t = timer( 5 ); # or... $t = IO::Run::Timer->new( 5 ); run $t, ...; ## A timeout (which is a timer that dies on expiry): $t = timeout( 5 ); # or... $t = IO::Run::Timer->new( 5, exception => "harness timed out" ); DESCRIPTIONThis class and module allows timers and timeouts to be created for use by IPC::Run. A timer simply expires when it's time is up. A timeout is a timer that throws an exception when it expires. Timeouts are usually a bit simpler to use than timers: they throw an exception on expiration so you don't need to check them: ## Give @cmd 10 seconds to get started, then 5 seconds to respond
my $t = timeout( 10 );
$h = start(
\@cmd, \$in, \$out,
$t,
);
pump $h until $out =~ /prompt/;
$in = "some stimulus";
$out = '';
$t->time( 5 )
pump $h until $out =~ /expected response/;
You do need to check timers: ## Give @cmd 10 seconds to get started, then 5 seconds to respond
my $t = timer( 10 );
$h = start(
\@cmd, \$in, \$out,
$t,
);
pump $h until $t->is_expired || $out =~ /prompt/;
$in = "some stimulus";
$out = '';
$t->time( 5 )
pump $h until $out =~ /expected response/ || $t->is_expired;
Timers and timeouts that are reset get started by start() and pump(). Timers change state only in pump(). Since run() and finish() both call pump(), they act like pump() with respect to timers. Timers and timeouts have three states: reset, running, and expired. Setting the timeout value resets the timer, as does calling the reset() method. The start() method starts (or restarts) a timer with the most recently set time value, no matter what state it's in. Time valuesAll time values are in seconds. Times may be any kind of perl number, e.g. as integer or floating point seconds, optionally preceded by punctuation-separated days, hours, and minutes. Examples: 1 1 second 1.1 1.1 seconds 60 60 seconds 1:0 1 minute 1:1 1 minute, 1 second 1:90 2 minutes, 30 seconds 1:2:3:4.5 1 day, 2 hours, 3 minutes, 4.5 seconds 'inf' the infinity perl special number (the timer never finishes) Absolute date/time strings are *not* accepted: year, month and day-of-month parsing is not available (patches welcome :-). Interval fudgingWhen calculating an end time from a start time and an interval, IPC::Run::Timer instances add a little fudge factor. This is to ensure that no time will expire before the interval is up. First a little background. Time is sampled in discrete increments. We'll call the exact moment that the reported time increments from one interval to the next a tick, and the interval between ticks as the time period. Here's a diagram of three ticks and the periods between them: -0-0-0-0-0-0-0-0-0-0-1-1-1-1-1-1-1-1-1-1-2-...
^ ^ ^
|<--- period 0 ---->|<--- period 1 ---->|
| | |
tick 0 tick 1 tick 2
To see why the fudge factor is necessary, consider what would happen when a timer with an interval of 1 second is started right at the end of period 0: -0-0-0-0-0-0-0-0-0-0-1-1-1-1-1-1-1-1-1-1-2-...
^ ^ ^ ^
| | | |
| | | |
tick 0 |tick 1 tick 2
|
start $t
Assuming that check() is called many times per period, then the timer is likely to expire just after tick 1, since the time reported will have lept from the value '0' to the value '1': -0-0-0-0-0-0-0-0-0-0-1-1-1-1-1-1-1-1-1-1-2-...
^ ^ ^ ^ ^
| | | | |
| | | | |
tick 0 |tick 1| tick 2
| |
start $t |
|
check $t
Adding a fudge of '1' in this example means that the timer is guaranteed not to expire before tick 2. The fudge is not added to an interval of '0'. This means that intervals guarantee a minimum interval. Given that the process running perl may be suspended for some period of time, or that it gets busy doing something time-consuming, there are no other guarantees on how long it will take a timer to expire. SUBCLASSINGINCOMPATIBLE CHANGE: Due to the awkwardness introduced by ripping pseudohashes out of Perl, this class no longer uses the fields pragma. FUNCTIONS & METHODS
TODOuse Time::HiRes; if it's present. Add detection and parsing of [[[HH:]MM:]SS formatted times and intervals. AUTHORBarrie Slaymaker <barries@slaysys.com>
|