Solved

setting a timeout

Posted on 1998-09-09
6
226 Views
Last Modified: 2010-03-05
Here is a copy of a admin at my hosting copany "I am thinking because it involves uploading the program is just not
executing but yet hanging around and keeping httpd childs open until we kill
them off.  Could you set a timeout or somethiong on this script?"  How would I do this?  Here is the script.  Thanks for any suggestions/help.
#!/usr/local/bin/perl
    use CGI;
      require '/home/dank/public_html/cgi-local/chat/locksubs.pl';

    $cgi = new CGI;

print "Content-type: text/html\n\n";


    if ($cgi->param('file') eq '') {
      &no_pic;
}
      $username = $cgi->param('USERNAME');
      $pics_dir = '/home/dank/public_html/cgi-local/pics';
    $filename = "/home/dank/public_html/cgi-local/pics/$username.gif";
    open(OUT,"> $filename");
    while($bytesread = read($cgi->param('file'), $buffer, 1024)) {
        print OUT $buffer;
    }
    close(OUT);
      &lock("waiting-count.db","$pics_dir",'3');
      open(COUNT,"<$pics_dir/waiting-count.db");
      $count = <COUNT>;
      close(COUNT);
      $count++;
      open(COUNT,">$pics_dir/waiting-count.db");
      print COUNT "$count";
      close(COUNT);
      &unlock("waiting-count.db","$pics_dir");      
      &lock("waiting.db","$pics_dir",'3');
      open(WAIT,">>$pics_dir/waiting.db");
      print WAIT "$username--$count\n";
      close(WAIT);
      &unlock("waiting.db","$pics_dir");

print "done. username = $username\n";

sub no_pic {
      print "sorry please give a pic to process.\n";
      exit;
}
0
Comment
Question by:idadan
  • 3
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1204782
What is &lock
0
 

Author Comment

by:idadan
ID: 1204783
A file locking routine, from the cgi-perl cookbook (matt wright)...
0
 

Accepted Solution

by:
arhnold earned 100 total points
ID: 1204784
Hello,
      One method I use when I want to have a timeout on some part of a script is to use SIGALRM, with the signal handler set to a subroutine to handle the case.  Here is an example hacked from a program of mine...

..yada yada yada...
$timeout = 60;
$SIG{'ALRM'} = 'Wait_For_Timeout'; # A subroutine
alarm($timeout)  # Starts the alarm timer ticking down
   # Main reading loop.
   while ($go_on) {
      &Go_Do_Something(); # This has $timeout seconds to return
                          # else the ALRM will pop  
      # We returned in time, restart the timer.              
      alarm($timeout);
      ... yada yada yada...


## In this case here is what the signal handler would do...
sub Wait_For_Timeout {
   #####################################
   # Signal Handler for Wait_For (ALRM).
   #####################################
   &dprint("\n\t** Script Timeout.  Moving on...\n");
   $go_on=0;  # Breaks the while() loop above
}

      Hope this helps.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:idadan
ID: 1204785
Ok... in this line
    $SIG{'ALRM'} = 'Wait_For_Timeout'; # A subroutine
is that legal? It looks funny to me.. and also shouldn't you define $go_on as something before calling it or won't it just read that as 0?  I need this code to work as is, if there is anything wrong, could you just look it over and check it?  thanks.
0
 

Expert Comment

by:arhnold
ID: 1204786
Yes this is legal.  If you have the Programming Perl v2 book look at p340-1 for an example.  The only difference is that they use
$SIG{'x'} = \&foo, but its the same thing in the end.  

      The $go_on variable was defined a couple hundred lines previous in the script.  It was declared as a 'my' and was visible to all code in this file.

      You could keep your code pretty much the same.  Write a new subroutine to cleanup the locks on a timeout and print an error message.  Then wrap the area of the code you wish to guard with the timer stuff. e.g

$SIG{'ALRM'} = \&My_Timeout;

alarm(60);
&lock("waiting.db","$pics_dir",'3');
   open(WAIT,">>$pics_dir/waiting.db");
   print WAIT "$username--$count\n";
   close(WAIT);
   &unlock("waiting.db","$pics_dir");
## We completed this part in under 60 seconds, cancel the timer
alarm(0);

$SIG{'ALRM'} = 'DEFAULT'; # Return to normal response
0
 

Author Comment

by:idadan
ID: 1204787
Thanks for the help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question