I am currently working on a project that involves crawling certain websites. However sometimes my Perl program will get "stuck" on a website for some reason (can't figure out why) and the program will freeze for hours. To get around this I inserted some code to time out on the subroutine that crawls the webpage. The problem with this is that, lets say I set the alarm to 60 sec, most of the time the page will timeout correctly, but occasionally the program will not time out and just sit for hours on end (maybe forever since I usually kill the program).
On the really bad websites the Perl program will just eat through my memory, taking 2.3GB of RAM and 13GB of swap. Also the CPU usage will be high, and my computer will be sluggish. Luckily if it times out all the resources get released quickly.
Is this my code or a Perl issue? What should I correct and why was it causing this problem?
Thanks
Here is my code:
eval {
local $SIG{ALRM} = sub { die("alarm\n") };
alarm 60;
&parsePageFunction();
alarm 0;
};#eval
if($@) {
if($@ eq "alarm\n") { print("Webpage Timed Out.\n\n"); }#if
else { die($@."\n"); }#else
}#if