Click here to get back home

Perl CGI Issue

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Perl CGI Issue Eric 06-06-2008
Get Chitika Premium
Posted by Dan Rumney on June 6, 2008, 5:21 pm
Please log in for more thread options
Eric wrote:
[snip]

> # Redirect the user to the appropriate output page.
> $io = new IO::Handle;
> if ($io->fdopen(fileno(STDOUT),"w")) {
> $io->print("Output:output.php?load=$sum/$pdbid.html\n");
> $io->flush() || die $!;
> }
>
> system("cmd & perl backend.pl $pdbid $sum"); # takes about 4
> minutes to run
>
> My web page makes an AJAX post to this cgi script to start the
> "backend" process. Since that process takes a few minutes, I would
> like the user to be taken to a waiting page. However, I need to pass
> that $sum variable back to the page.

[snip]

> In actuality what happens is there is a delay of several minutes
> before the output message is sent to the page. However, it is not the
> full amount of time required to run backend.pl. It seems to be pretty
> consistently about half of the required time. The page then gets the
> output message, and redirects to the waiting page and works fine from
> thereon out.

[snip]

Having thought about this further, I think your problem may have more to
do with how web servers serve pages and how clients handle webserver
responses.

IIRC, the default timeout for a webserver is something in the region of
2 minutes. If a request for a resource (in this case, your dynamic page)
is not completed before the timeout is up, then the server will close
the output stream.

On the client-side, your AJAX request will sit there, waiting for the
request to complete before it changes to readyState=4 (loaded) and I
would bet you a shekel that your code waits until readyState=4 before it
does anything with it.

If the above is true then that would probably explain why the client
waits half the backend run time (i.e. 2 minutes) before doing anything.

You can confirm the behaviour of your script by actually running it from
the command line. That will show you once and for all the point at which
the "Output:" string is being printed



Posted by xhoster on June 6, 2008, 6:34 pm
Please log in for more thread options
> Hello,
>
> I'm having an issue with a perl CGI script. The code looks like this:
>
> #!/usr/bin/perl
> use CGI;
> use IO::Handle;
>
> $|=1;
>
> [... snip ...]
>
> # Redirect the user to the appropriate output page.
> $io = new IO::Handle;
> if ($io->fdopen(fileno(STDOUT),"w")) {
> $io->print("Output:output.php?load=$sum/$pdbid.html\n");
> $io->flush() || die $!;
> }
>
> system("cmd & perl backend.pl $pdbid $sum"); # takes about 4
> minutes to run
>
> My web page makes an AJAX post to this cgi script to start the
> "backend" process. Since that process takes a few minutes, I would
> like the user to be taken to a waiting page. However, I need to pass
> that $sum variable back to the page.
>
> In my head, this code should print the output message to stdout, start
> the backend.pl script in the background,

"cmd &" looks like something meant for Windows. But Perl on my Windows
machine, that doesn't cause the thing to be started in the background.
Are you sure your system does what you want it to? I almost never use
Perl on windows, so I could be misinterpreting something.

> exit and then send the
> contents of stdout down to the web page.

But if it isn't really in the background, then your program won't exit.
If your program did exit immediately after starting the program in the
background, then you would not need to take any special measures to flush
the buffer. It would automatically get flushed upon exit.

>
> In actuality what happens is there is a delay of several minutes
> before the output message is sent to the page. However, it is not the
> full amount of time required to run backend.pl. It seems to be pretty
> consistently about half of the required time. The page then gets the
> output message, and redirects to the waiting page and works fine from
> thereon out.

It could be that you are flushing the buffer to the web-server, but the
web-server isn't passing it on to the web-client immediately, or that the
web-client AJAX program isn't acting on the information immediately. They
might be waiting for X minutes, or for Y bytes, or for eof, before *they*
act, and are insensitive to your program's buffer-flushing. If STDOUT gets
closed, not just flushed, that might encourage them to hurry things up.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by Ben Morrow on June 6, 2008, 8:18 pm
Please log in for more thread options

Quoth xhoster@gmail.com:
> >
> > system("cmd & perl backend.pl $pdbid $sum"); # takes about 4
> > minutes to run
>
> "cmd &" looks like something meant for Windows. But Perl on my Windows
> machine, that doesn't cause the thing to be started in the background.
> Are you sure your system does what you want it to? I almost never use
> Perl on windows, so I could be misinterpreting something.

My suspicion is the OP has misinterpreted the line

You could also use

system("cmd &")

in perldoc -q background. I have no idea why it 'worked' at all, unless
the OP *is* on windows, where the command shell treats '&' as a
statement separator, much like ';' in sh.

To the OP: that statement means you should run

system("perl backend.pl $pdbid $sum &");

and it only works on systems with a shell that interprets '&' that way.
A much better solution is IPC::Run.

Ben

--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
'Alcestis') [ flame, and falls out of sight. ] ben@morrow.me.uk

Posted by A. Sinan Unur on June 7, 2008, 8:21 am
Please log in for more thread options

>
> Quoth xhoster@gmail.com:
>> >
>> > system("cmd & perl backend.pl $pdbid $sum"); # takes about 4
>> > minutes to run
>>
>> "cmd &" looks like something meant for Windows.

...

>
> My suspicion is the OP has misinterpreted the line
>
> You could also use
>
> system("cmd &")
>
> in perldoc -q background. I have no idea why it 'worked' at all,
> unless the OP *is* on windows, where the command shell treats '&' as a
> statement separator, much like ';' in sh.

It does. Good catch.

> To the OP: that statement means you should run
>
> system("perl backend.pl $pdbid $sum &");

system( 'start', 'perl', 'backend.pl', $pdbid, $sum );

might be the equivalent.

Sinan

--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Posted by Bill H on June 6, 2008, 9:00 pm
Please log in for more thread options
> Hello,
>
> I'm having an issue with a perl CGI script. =A0The code looks like this:
>
> #!/usr/bin/perl
> use CGI;
> use IO::Handle;
>
> $|=3D1;
>
> [... snip ...]
>
> # Redirect the user to the appropriate output page.
> $io =3D new IO::Handle;
> if ($io->fdopen(fileno(STDOUT),"w")) {
> =A0 =A0 $io->print("Output:output.php?load=3D$sum/$pdbid.html\n");
> =A0 =A0 $io->flush() || die $!;
>
> }
>
> system("cmd & perl backend.pl $pdbid $sum"); =A0 =A0# takes about 4
> minutes to run
>
> My web page makes an AJAX post to this cgi script to start the
> "backend" process. =A0Since that process takes a few minutes, I would
> like the user to be taken to a waiting page. =A0However, I need to pass
> that $sum variable back to the page.
>
> In my head, this code should print the output message to stdout, start
> the backend.pl script in the background, exit and then send the
> contents of stdout down to the web page.
>
> In actuality what happens is there is a delay of several minutes
> before the output message is sent to the page. =A0However, it is not the
> full amount of time required to run backend.pl. =A0It seems to be pretty
> consistently about half of the required time. =A0The page then gets the
> output message, and redirects to the waiting page and works fine from
> thereon out.
>
> Does anyone know how I can get that output message to my webpage
> immediately? =A0As you can see I've tried flushing stdout several
> different ways with no luck.
>
> Also, if there is a better group for me to post to, that would be
> appreciated too.
>
> Thanks,
> Eric

It's been a long day but I think I get the flow of this

Client - > perl - > php - > perl - > client

If this is the case and the 2 perl's in the above are the same program
either 1)eliminate the php and do what it does in perl so you can send
the status back to the client as it is being performed or 2) do it all
in php (same as #1) or 3)have your php call the perl when it is done
so perl isn't waiting and the client isn't waiting.

I think Sinan was close on the suggested link in his first response.

Bill H

Similar ThreadsPosted
Perl performance issue July 27, 2005, 8:47 pm
perl array issue October 26, 2005, 7:47 pm
ASP.Net App Issue... Maybe Resolved via PERL? June 8, 2007, 2:47 pm
Perl LWP Issue with JSP's April 16, 2008, 2:29 pm
Perl User Name/Password issue April 1, 2005, 2:09 am
perl MySQL using DBI - security issue April 12, 2006, 4:24 am
DB Module installation issue in Perl April 21, 2007, 7:28 pm
A perl issue when execute system call July 19, 2007, 7:42 am
The Perl Review, Spring 2006: The Sudoku Issue February 28, 2006, 2:34 pm
Curl/Perl http post performanc issue August 2, 2006, 10:43 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap