eversuhoshin has asked for the wisdom of the Perl Monks concerning the following question:
Dear monk, I need help with timing out. I have a script that extracts excerpts from financial statements. The basic format is the following.
foreach (@files){ my $excerpt; my $match; my $start1 = time; #measure time for each file my $duration1; my $data = slurp($_); if($data!~m/table\s?of\s?contents?|\sindex\spart\s(?:1|I)/i){ if($data=~m/(?:Item|ITEM)[Ss]?\s?(?:\.|\-|:|\-\-|\,)?\s?(?:1|I +)\s?(?:\.|\-|\:|\-\-|\,)?\s?(?:Description|DESCRIPTION)?\s?(?:[Oo][Ff +])?\s?(?:[Tt][Hh][Ee])?\s?(?:Busine\s?ss|BUSINE\s?SS|Company|COMPANY) +\s?(?:\.|\-|:|\-\-|\,|\()? (.*?)\s?(?:Item|ITEM)[Ss]?\s?(?:\.|\:|\-\-|\-|\,)? +\s?(?:I|1A|1B|2)\s?(?:\.|\:|\-\-|\-|\,)?/x){ $excerpt=$1; $match=3; goto record; } if($data=~m/(?:Business\s?Development|BUSINESS\s?DEVELOPMENT)\ +s? (.*?)\s?(?:Item|ITEM)[Ss]?\s?(?:\.|\-|:|\-\-|\,)?\ +s?(?:I|1A|1B|2)\s?(?:\.|\:|\-\-|\-|\,)?/x){ $excerpt=$1; $match=4; goto record; } if($data=~m/(?:PART|Part)\s?(?:\.|\-|\:|\-\-|\,)?\s?(?:I|1)\s? +(?:\.|\-|:|\-\-|\,)?\s?(?:BUSINESS|Business|GENERAL|general) (.*?)\s?(?:Item|ITEM)[Ss]?\s?(?:I|1A|1B|2|3)\s?(?: +\:|\-|\,|\-\-|\.|\,)?/x){ $excerpt=$1; $match=5; goto record; } if($data=~m/(?:Item|ITEM)?[Ss]?\s?1\s?(?:\.|\-|\:|\-\-|\,)?(?: +\.|\-|\:|\-\-|\,)?\s?(?:1A)?\s?(?:\.|\-|\:|\-\-|\,)?\s?(?:AND|[Aa]nd| +\&)\s?2\s?(?:\.|\:|\-|\-\-|\,)?\s?(?:\.|\-|\:|\-\-|\,)?\s? (?:[Bb]usiness\s?\,\s?[Rr]isks?\s?[Ff]actors\s?(?:[Aa] +nd|AND|\&)\s?[Pp]roperties|BUSINESS\s?\,\s?RISK\s?FACTORS?\s?AND\s?PR +OPERTIES) (.*?)\s?(?:Item|ITEM)\s?[Ss]?\s?(?:\:|\-|\,|\-\-|\.)?\ +s?(?:1A|1B|2|I|3)\s?(?:\.|\:|\-|\-\-|\,)?/x){ $excerpt=$1; $match=7; goto record; } if($data=~m/(?:Item|ITEM)[Ss]?\s?(?:\.|\-|:|\-\-|\,)?\s?(?:1|I +)\s?(?:\.|\-|:|\-\-|,)?\s?(?:BUSINESS|[Bb]usiness)\s?(?:\.|\-|\:|\,)? (.*?)\s?(?:Item|ITEM)\s?(?:\.|\:|\-|\-\-|\,)?\s?(? +:I|1A|1B|2)\s?(?:\.|\:|\-|\-\-|\,)?/x){ $match=8; $excerpt=$1; goto record; } } record: if(defined($excerpt)){ $excerpt=~s/\s{2,}|\.\s|\"|\(|\)|\,|\'|\r|\n/ /g; $excerpt=~s/^\s+|\s+$//; #trim $duration1 = ceil((time - $start1)/60); #measure exec +ution time for each file until printing print "$match \n $duration1 \n $excerpt \n"; } }
a lot of times the code gets stuck because it takes time to extract the excerpt as some files are over 10MB. I want to use a timeout function to move to the next element if it takes more than two minutes. I have looked up Alarm but I don't know how to incorporate it into my code. It would be great if you could help me use a timeout in my function in case it gets stuck.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help with timeout
by kcott (Archbishop) on Sep 25, 2012 at 04:54 UTC | |
|
Re: Help with timeout
by Marshall (Canon) on Sep 25, 2012 at 19:37 UTC | |
|
Re: Help with timeout
by Anonymous Monk on Sep 25, 2012 at 00:36 UTC | |
by eversuhoshin (Sexton) on Sep 25, 2012 at 02:37 UTC | |
by grizzley (Chaplain) on Sep 25, 2012 at 06:46 UTC |