Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I've isolated all the code that can take a while and return all the output to the user first. However, it seems like that doesn't always work. This morning I was also testing file locks and noticed that same problem, no output until the script completes. Thankfully, the file lock seems rock solid. However, is there some magic thing that would force the broswer to output each as it gets it?
Here's a sample of the code that I'm testing:
#!/usr/bin/perl -w $|=1; ## Don't buffer output use CGI; use Fcntl qw(:flock); my $file = 'tmp/test_lock.txt'; my $SEMAPHORE = $file . '.lck'; # Output Early for debug my $q = new CGI; print $q->header; my $x; for ($x = 0; $x < 50; $x++) { print "<!-- FORCE STREAM -->\n"; } for ($x = 0; $x < 10; $x++) { print "Line $x<BR>\n"; sleep 1; } print "Getting lock<br>\n"; open(S, ">$SEMAPHORE") or die "$SEMAPHORE: $!"; flock(S, LOCK_EX) or die "flock() failed for $SEMAPHORE: $!"; print "Got lock<br>\n"; my $l; my @in; if (open (FH, "$file")) { @in = <FH>; close FH; foreach $_ (@in) { if ($_ =~ /(.*): I have/) { $l = $1; } } $l = $l+1 if $l; } else { print "Can't open $file: $!"; } $l = 1 unless $l; push (@in,"$l: I have written ($$) flock\n"); open (FH, ">$file") or die "Can't open $file: $!"; print "About to write<br>\n"; print FH @in; print "Written: $l<br>\n"; close FH; print "Going to sleep...\n"; for ($x = 0; $x < 50; $x++) { print "$x ... <!-- FORCE STREAM --><br>\n"; sleep 1; } print "Woken up...<br>\n"; close S;
Yes, I know that I could simplify the file parsing, but I wanted to simulate looking for a specifc line in a large file and having a fairly big chunk of open time to be sure that the file didn't get clobbered. The issue is output. I'd like to see the number 0 to 49 poping up once a second. But what I see is nothing for a minute and then all the output at once.
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Forcing output to a browser when perl is busy
by ccn (Vicar) on Jul 17, 2004 at 19:49 UTC | |
by Anonymous Monk on Jul 17, 2004 at 20:28 UTC | |
by hbo (Monk) on Jul 17, 2004 at 20:45 UTC | |
by Anonymous Monk on Jul 17, 2004 at 22:40 UTC | |
by ccn (Vicar) on Jul 17, 2004 at 20:45 UTC | |
by hsinclai (Deacon) on Jul 17, 2004 at 22:18 UTC | |
by bart (Canon) on Jul 18, 2004 at 15:38 UTC | |
by Anonymous Monk on Jul 20, 2004 at 01:40 UTC | |
|
Re: Forcing output to a browser when perl is busy
by hbo (Monk) on Jul 17, 2004 at 20:16 UTC |