Perl and waiting for execution of external program

G

Gunnar Hjalmarsson

It sometimes happens that Perl does not wait for the (lengthy)
calculations to finish, but tries to close the program prematurely. (At
least, that is what the symptoms look like).

How is the program invoked? Is it possibly run as a CGI script?
 
J

jonas.andersson

Hi,

Sometimes, when running an external program through Perl, I run into
problems:

....
open (PROG, "|$MyProgram &> /dev/null") or die "cannot open
program"

print PROG "Load file $FileName\n";
print PROG "Calculate all\n";

close (PROG) or die "cannot close program";
....

It sometimes happens that Perl does not wait for the (lengthy)
calculations to finish, but tries to close the program prematurely. (At
least, that is what the symptoms look like).

Is there a way to force Perl to wait for the program to finish before
it tries to close it, however long it takes?

Thanks for your time,

JA
 
J

jonas.andersson

How is the program invoked? Is it possibly run as a CGI script?

You are correct: it is run as a CGI script. Sorry for not mentioning
that.

/ JA
 
C

Chris Mattern

Hi,

Sometimes, when running an external program through Perl, I run into
problems:

...
open (PROG, "|$MyProgram &> /dev/null") or die "cannot open
program"

print PROG "Load file $FileName\n";
print PROG "Calculate all\n";

close (PROG) or die "cannot close program";
...

It sometimes happens that Perl does not wait for the (lengthy)
calculations to finish, but tries to close the program prematurely. (At
least, that is what the symptoms look like).
What the symptoms actually *are* would be more useful in solving
the problem then your interpretation of what they "look like."
You should be providing the first instead of the second, rather
than the other way around.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
X

xhoster

Hi,

Sometimes, when running an external program through Perl, I run into
problems:

...
open (PROG, "|$MyProgram &> /dev/null") or die "cannot open
program"

print PROG "Load file $FileName\n";
print PROG "Calculate all\n";

close (PROG) or die "cannot close program";
...

It sometimes happens that Perl does not wait for the (lengthy)
calculations to finish, but tries to close the program prematurely. (At
least, that is what the symptoms look like).

Is there a way to force Perl to wait for the program to finish before
it tries to close it, however long it takes?

Most likely your http server is getting impatient and clobbering your
Perl script. One possible non-Perl solution is to make your http server
more patient. Another possible solutions is to have your Perl program
fork, have the parent finish pronto, while the child (after closing STDOUT)
keeps going, safe from the wrath of impatient http server. The problem
then is that the web browser sees the results of the parent's output, and
not the child's. If the child's results need to get to the web browser,
you will have to arrange for that rendovous yourself.

Xho
 
J

jonas.andersson

What the symptoms actually *are* would be more useful in solving
the problem then your interpretation of what they "look like."
You should be providing the first instead of the second, rather
than the other way around.

OK,

here are the symptoms:

$linenumber=796;
open (PROG, "|$MyProgram &> /dev/null") or &err;

print PROG "Load file $FileName\n";
print PROG "Calculate all\n";
$linenumber=801;
close (PROG) or &err;
$linenumber=$803;

....

sub err {

print "error - last line to be noted was $linenumber \n";
exit;
}


The symptoms being that it sometimes ends with sub err printing 801,
suggesting that it ended on the line 'close (PROG) or &err;', ie
failure to close the program [premature close attempt?]. This does not
happen when I run the Perl script by hand, but it does sometimes happen
when I run it as CGI or with crontab. The Apache timeout, as far as I
can tell, is more allowing than the time it takes for the script to
halt prematurely (and does not explain the crontab issue). Any other
ideas?

Thanks for your time,

JA
 
G

Gunnar Hjalmarsson

here are the symptoms:

$linenumber=796;
open (PROG, "|$MyProgram &> /dev/null") or &err;

print PROG "Load file $FileName\n";
print PROG "Calculate all\n";
$linenumber=801;
close (PROG) or &err;
$linenumber=$803;

...

sub err {

print "error - last line to be noted was $linenumber \n";
exit;
}

You'd better make use of the $! variable to grab the *reason* for the
failure:

open PROG, "|$MyProgram &> /dev/null"
or err("Error - couldn't open pipe to $MyProgram: $!");
print PROG "Load file $FileName\n";
print PROG "Calculate all\n";
close PROG or err("Error - couldn't close pipe: $!");

sub err { print shift; exit 1 }
 
A

A. Sinan Unur

(e-mail address removed) wrote in @f14g2000cwb.googlegroups.com:
here are the symptoms:

Something tells me you have made a delicious spaghetti and I do not have
the inclination to go through it. You should read the posting guidelines
which contains invaluable suggestions on how to help others help you.
$linenumber=796;

$linenumber = __LINE__;
open (PROG, "|$MyProgram &> /dev/null") or &err;

open my $prog, "|MyProgram &> /dev/null"
or die $!;
$linenumber=$803;

What is $803?

Sinan.
 
X

xhoster

OK,

here are the symptoms:

$linenumber=796;
open (PROG, "|$MyProgram &> /dev/null") or &err;

print PROG "Load file $FileName\n";
print PROG "Calculate all\n";
$linenumber=801;
close (PROG) or &err;
$linenumber=$803;

...

sub err {

print "error - last line to be noted was $linenumber \n";
exit;
}

Why have such a pointless err subroutine? die will automatically
tell you the line number, with greater precision, and without polluting
your code with the global variable $linenumber.

The symptoms being that it sometimes ends with sub err printing 801,
suggesting that it ended on the line 'close (PROG) or &err;', ie

Instead of asking us why the close is failing, try asking your computer.
That is what $! is for.
failure to close the program [premature close attempt?].

I've never heard of that error.

Most likely your pipe open failed, but perl doesn't realize this until
close is called. I would guess it is a permission or path issue with
the external program. But use $! to find out.

Xho
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in @mamenchi.zrz.TU-Berlin.DE:
Ah, you know what it is. Rarely defined, but valid.

It was more of a rhetorical question :) I was feeling very contemplative
at the time.

Sinan.
 
A

Anno Siegel

A. Sinan Unur said:
(e-mail address removed)-berlin.de (Anno Siegel) wrote in @mamenchi.zrz.TU-Berlin.DE:


It was more of a rhetorical question :) I was feeling very contemplative
at the time.

I can see that...

$803? Oh, it should be 803, not $803.
Wait, it shouldn't be hard-coded, it should be __LINE__.
Ah, but then it shouldn't be there at all, die() or warn() do that better.

Quite some contemplation on a single variable :)

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top