Gossamer Forum
Home : General : Perl Programming :

How to delay sub until prev. sub is complete

Quote Reply
How to delay sub until prev. sub is complete
The script below creates a text file (input file) in a directory that is polled for files every 0.5 seconds. When it sees a file it reads it, processes the commands, then deletes the input file and creates an output file with the results.

The way it is written below it creates the input file and then goes directly to the next sub that creates a web page that has the contents of the output file embedded in it. But it is creating the web page before the output file is available.

What can I do to make the print_html sub wait until the output file is available before printing it? The complete script is located at: http://www.loadfinders.com/test/routing.txt

Code:
$admin_email = 'mileage@LoadFinders.com';
$side_bar = 'D:\home\routing\www\cgi-bin\includes\sidebar.txt';
$rt_url = 'D:\home\routing\www\pmx';
$footer = 'D:\home\routing\www\cgi-bin\includes\footer.txt';
$session = $$.time;
$report = 'D:\home\routing\www\pmx\output_rt$session.out';

&parse_form;

open(ROUTE, ">>$rt_url/input_rt$session.in");
print ROUTE "-rcd -rs -p -ob -op\n$form{'c0'}, $form{'s0'}*$form{'c1'}, $form{'s1'}\n";
close(ROUTE);

#
# NEED SOME SORT OF DELAY OR FILE CHECK ADDED HERE
#

&print_html;

Thanks!

Jimmy Crow
http://www.homewithgod.com/
Quote Reply
Re: [jcrow] How to delay sub until prev. sub is complete In reply to
I have changed the coding up a little and got it to work using sleep 3; but that is not reliable because the processing time varies. I have tried to do an if exists and a wait routine but could not get the code right.

This is what I currently have:

Code:
$admin_email = 'mileage@LoadFinders.com';
$side_bar = 'D:\home\routing\www\cgi-bin\includes\sidebar.txt';
$rt_url = 'D:\home\routing\www\pmx';
$footer = 'D:\home\routing\www\cgi-bin\includes\footer.txt';
$session = $$.time;

&parse_form;

open(ROUTE, ">>$rt_url/input_rt$session.in");
print ROUTE "-rcd -rs -p -ob -op\n$form{'c0'}, $form{'s0'}*$form{'c1'}, $form{'s1'}\n";
close(ROUTE);

&print_html;

sub print_html {
# --------------------------------------------------------
# Print out the headers if they haven't already been printed.

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

&print_top_html;

sleep 3;

&report_html;

&print_bottom_html;

exit;
}

The complete script is at: http://www.loadfinders.com/test/routing.txt

I am thinking a wait would work best is someone could help with the code on it.

Thanks.

Jimmy Crow
http://www.homewithgod.com/

Last edited by:

jcrow: Oct 10, 2001, 10:24 AM
Post deleted by jcrow In reply to
Post deleted by jcrow In reply to
Quote Reply
Re: [jcrow] How to delay sub until prev. sub is complete In reply to
This what I came up with to get the script to wait until the file is available and also check for an error file before printing to the page, but it's not a 100% foolproof.

The program takes the input, then processes it and sends out an output file ($route). If there is an error in the input, it creates an empty output file ($route) and then an error report ($err_report). A few empty output files have slipped past it and printed to the page. Any suggestions on how to stop this?

Code:

&parse_form;
&input_report;
&sleep;

sub sleep {
# --------------------------------------------------------

$out_path = 'D:\home\routing\www\pmx\output_rt';
$route = "$out_path$session.out";
$exists = '1' if -e "$route";

sleep 1;

if ($exists eq '1') {
if (-z $route) { &check_error; }
else { &print_html; }
}
else { &check_error; }
}


sub check_error {
# --------------------------------------------------------

$err_path = 'D:\home\routing\www\pmx\error_rt';
$err_report = "$err_path$session.err";
$exists = '1' if -e "$err_report";

if ($exists eq '1') {
&print_top_html;
&report_html;

print qq~
<font color="#FF0000"><pre>
~;

open(MN, "$err_report");
@mn = <MN>;
print ("@mn");
close(MN);

print qq~
</pre></font>
~;

&print_bottom_html;
exit;
}

else{
&sleep;
}
}

Thanks.
Jimmy Crow
http://www.homewithgod.com/