Perl and waiting for execution of external program

Discussion in 'Perl Misc' started by Gunnar Hjalmarsson, Jan 19, 2005.

  1. wrote:
    > 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?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 19, 2005
    #1
    1. Advertising

  2. Gunnar Hjalmarsson

    Guest

    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
     
    , Jan 19, 2005
    #2
    1. Advertising

  3. Gunnar Hjalmarsson

    Guest

    >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
     
    , Jan 19, 2005
    #3
  4. wrote:

    > 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?"
     
    Chris Mattern, Jan 19, 2005
    #4
  5. Gunnar Hjalmarsson

    Guest

    wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jan 19, 2005
    #5
  6. Gunnar Hjalmarsson

    Guest

    > 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
     
    , Jan 20, 2005
    #6
  7. wrote:
    > 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 }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 20, 2005
    #7
  8. wrote in news:1106236867.183882.199950
    @f14g2000cwb.googlegroups.com:

    >> 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.


    > 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.
     
    A. Sinan Unur, Jan 20, 2005
    #8
  9. Gunnar Hjalmarsson

    Guest

    wrote:
    > > 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;
    > }


    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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jan 20, 2005
    #9
  10. Gunnar Hjalmarsson

    Anno Siegel Guest

    A. Sinan Unur <> wrote in comp.lang.perl.misc:

    > What is $803?


    Ah, you know what it is. Rarely defined, but valid.

    Anno
     
    Anno Siegel, Jan 21, 2005
    #10
  11. -berlin.de (Anno Siegel) wrote in news:csrnc3$r8n$2
    @mamenchi.zrz.TU-Berlin.DE:

    > A. Sinan Unur <> wrote in comp.lang.perl.misc:
    >
    >> What is $803?

    >
    > 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. Sinan Unur, Jan 21, 2005
    #11
  12. Gunnar Hjalmarsson

    Anno Siegel Guest

    A. Sinan Unur <> wrote in comp.lang.perl.misc:
    > -berlin.de (Anno Siegel) wrote in news:csrnc3$r8n$2
    > @mamenchi.zrz.TU-Berlin.DE:
    >
    > > A. Sinan Unur <> wrote in comp.lang.perl.misc:
    > >
    > >> What is $803?

    > >
    > > 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.


    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
     
    Anno Siegel, Jan 21, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pat Deegan
    Replies:
    3
    Views:
    492
    Joe Smith
    Apr 22, 2004
  2. Replies:
    1
    Views:
    14,221
    Matt Humphrey
    Apr 22, 2005
  3. Daz
    Replies:
    5
    Views:
    344
  4. Sven Fischer
    Replies:
    2
    Views:
    583
    Roedy Green
    Mar 9, 2012
  5. Steffen Knobloch

    kick external script without waiting for it to end

    Steffen Knobloch, Jan 11, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    111
    Sherm Pendley
    Jan 11, 2005
Loading...

Share This Page