I've had problems for a while with users clicking on Post buttons multiple times because the system doesn't respond quickly. This is code for forum software, so it can result in mutiple posts. To counter that, I've added some duplicate post code to check, but that doesn't always work either esspecially if it's because the db has a lock that takes a while to clear.

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!


In reply to Forcing output to a browser when perl is busy by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.