Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

Limit perl code execution time

I need to limit the execution time of a particular line of code because it sometimes crashes the script:
      eval {$whois_info = `whois $domain`};
 
I would like the script to continue running even if it skips that particular line of code.

How do I accomplish this?
0
zylstra
Asked:
zylstra
  • 4
  • 3
1 Solution
 
Kim RyanIT ConsultantCommented:
A neat way would be to limit the amount of data whois returns, say by using the -rwhois-limit=x option.

This is hard to control from within Perl. Are you on linux? You can limit the CPU time for any process. For example, don't allow process to go for more than 6 minutes:
ulimit -t 360
0
 
ozoCommented:
You might try setting an alarm
perldoc -f alarm
0
 
zylstraAuthor Commented:
Ok, a little clarification:  first let's simplify the line to
$whois_info = `whois $domain`;

I am using perl installed on my Windows XP machine.
Sometimes the whois process hangs and ends the rest of the script, which I want to continue running.

teraplane, what's hard to control within perl, limiting the amount of data from whois, or limiting the time a particular piece of code takes to complete?

Example code would be helpful.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Kim RyanIT ConsultantCommented:
When you say `whois $domain` a new process is spawned to run the whois program.  If the whois has hung or suspended, perl has no knowledge of this. As ozo suggests you can get around this with alarms and signals.

Also perhaps this may behave better
$return_code = system("whois $domain")'
0
 
zylstraAuthor Commented:
I found the answer with alarm:

$SIG{ALRM} = \&timed_out;
eval {
    alarm (10);
    $whois_info = `whois $domain`;
    alarm(0);           # Cancel the pending alarm if whois responds.
};
if ($@ =~ /GOT TIRED OF WAITING/) {
    print " Whois timed out:  $@ ";
}


sub timed_out {
    die "GOT TIRED OF WAITING";
}
0
 
Kim RyanIT ConsultantCommented:
I think the comments here help point the user to the finding a solution. Can we not split some points?
0
 
zylstraAuthor Commented:
I believe the pointer to alarm sent me in the right direction, although I actually could not get the code to time out of the whois process which had hung.  I instead retrieved the whois info from another machine, and timed out the http process if it was taking to long.

From the Perl Cookbook: [system] doesn't gather the program's STDOUT like backticks or open.

How do I split points?
0
 
zylstraAuthor Commented:
Whatever, dudes...  I never got this thing to work.  I thought I did, but I didn't.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now