eval and alarm timeout for slow process (XML post)

2002-11-21 Thread jeff loetel
Does this look correct below. I know that I should test but due to the
environment that this program lives it is difficult track down problems.
It is a trigger off of a db. Can anyone take a few seconds to provide
feedback. Is there a betterway? The problem that I am trying to solve
here is a slow connection or a server down. I am using LWP and set the
timeout feature but it never really seemed to catch the situation. 

Any feedback is welcome.

eval {
  local $SIG{ALRM} = sub { die "timeout\n" };
  alarm (60);   # 60 sec. timeout

  # XML POST stuff here   
}

alarm (0);
if ($@ =~ /timeout/) {
print STDERR "$date_time XML POST timed out\n\n";
exit;
}

Jeff


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: eval and alarm timeout for slow process (XML post)

2002-11-22 Thread NYIMI Jose (BMB)
> -Original Message-
> From: jeff loetel [mailto:[EMAIL PROTECTED]] 
> Sent: Friday, November 22, 2002 9:01 AM
> To: Perl beginners
> Subject: eval and alarm timeout for slow process (XML post)
> 
> 
> Does this look correct below. I know that I should test but 
> due to the environment that this program lives it is 
> difficult track down problems. It is a trigger off of a db. 
> Can anyone take a few seconds to provide feedback. Is there a 
> betterway? The problem that I am trying to solve here is a 
> slow connection or a server down. I am using LWP and set the 
> timeout feature but it never really seemed to catch the situation. 
> 
> Any feedback is welcome.
> 
> eval {
>   local $SIG{ALRM} = sub { die "timeout\n" };
>   alarm (60);   # 60 sec. timeout
> 
>   # XML POST stuff here   
> }
> 
> alarm (0);

Try putting alarm(0) inside the eval block.

> if ($@ =~ /timeout/) {
> print STDERR "$date_time XML POST timed out\n\n";
> exit;
> }

See code below from "Oreilly Perl Cookbook".

> 
> Jeff
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

This comes from "Oreilly Perl Cookbook" :

Problem
You want to make sure an operation doesn't take more than a certain amount of time. 
For instance, you're running filesystem backups and want to abort if it takes longer 
than an hour. Or, you want to schedule an event for the next hour.

Solution
To interrupt a long-running operation, set a SIGALRM handler to call die. Set an alarm 
with alarm, then eval your code:

$SIG{ALRM} = sub { die "timeout" };

eval{
alarm(3600);
# long-time operations here
alarm(0);
};
if($@){
if($@=~/timeout/){
# timed out; do what you will here
}else{
alarm(0);   # clear the still-pending alarm
die;# propagate unexpected exception
} 
}

José.


 DISCLAIMER 

"This e-mail and any attachment thereto may contain information which is confidential 
and/or protected by intellectual property rights and are intended for the sole use of 
the recipient(s) named above. 
Any use of the information contained herein (including, but not limited to, total or 
partial reproduction, communication or distribution in any form) by other persons than 
the designated recipient(s) is prohibited. 
If you have received this e-mail in error, please notify the sender either by 
telephone or by e-mail and delete the material from any computer".

Thank you for your cooperation.

For further information about Proximus mobile phone services please see our website at 
http://www.proximus.be or refer to any Proximus agent.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]