in reply to Detecting Timeout of piped data
The usual way to handle this is easier than the thread mentioned by Zentara. There is an alarm that can interrupt program's execution when it counts down to zero seconds. See below for typical code. alrm_handler() is a subroutine that gets called when the alarm expires.
Note loop construction. I like to do it this way because I can see all 3 things that can cause the loop to stop all at once right there in the in the while() statement: (1) alarm, (2) no data (EOF), (3) or line text matches "DONE".
The alarm() is separated by a comma (yes, there is a comma operator). The "truth or falseness" of the of a comma joined statement is simply the last statement. This comma joined statement has the advantage that (1) no extra alarm() statement is needed before the while() statement and (2) if for some reason there are "next's" or various ways the loop can restart, I won't fail to restart the alarm (could happen if the alarm restart has to be embedded within the loop). Oh, and first thing to do within the body of the loop is to deactivate the alarm! Well unless of course the total time that you are trying to measure includes the processing time of the loop, but usually this is not the case.
$SIG{ALRM}=\&alrm_handler; sub alrm_handler { do...something here... sounds like die and cause shell command to be re-executed... } my $inLine; while (alarm(20), $inLine = <> and $inLine !~ /^Done/) { alarm(0); # deactivate the alarm!! normal loop here... } print "Exiting\n"; exit;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Detecting Timeout of piped data
by sriordan (Acolyte) on Aug 01, 2011 at 20:34 UTC | |
|
Re^2: Detecting Timeout of piped data
by Anonymous Monk on Aug 02, 2011 at 02:36 UTC | |
by Marshall (Canon) on Aug 02, 2011 at 06:57 UTC |