Redirection whatever be the forked life

Discussion in 'Perl Misc' started by Yohan N. Leder, May 15, 2006.

  1. Hi,

    Newbie in Perl and don't know how to do with this piece of script. I
    would like it redirects to mainboard.cgi without waiting for long forked
    process to be completed. Is it possible ? Knowing that in the real
    script the storit sub does a GET to a remote counter script which
    generates a log file.

    #!/usr/bin/perl -w
    use strict;

    # normally use CGI, but just a quick test script here
    print "CONTENT-TYPE: text/html; Charset=UTF-8\n\n";

    # ... here, some *quick* treatments (less than 10 seconds)...

    defined(my $pid = fork) or die "cannot fork : $!";
    unless ($pid) {
    # storit being a *long* process (say 1 or 2mn) without any print
    storit();
    exit 0;
    }

    # ... here, some checking about previous treatment

    print "Successfull treatment ! Redirection in 4 secondes...";
    print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"4; URL=mainboard.cgi\">";
    exit 0;

    sub storit {
    # simulate a long process (until 3mn in reality)
    for (my $idx=1;$idx<10;$idx++) {
    sleep 1;
    }
    }

    And with this mainboard.cgi :

    #!/usr/bin/perl -wT
    use strict;
    print "CONTENT-TYPE: text/html; Charset=UTF-8\n\n";
    print "... main board ...";
    exit 0;

    Yohan N. Leder
     
    Yohan N. Leder, May 15, 2006
    #1
    1. Advertising

  2. Yohan N. Leder

    David Squire Guest

    Yohan N. Leder wrote:
    > Hi,
    >
    > Newbie in Perl and don't know how to do with this piece of script. I
    > would like it redirects to mainboard.cgi without waiting for long forked
    > process to be completed. Is it possible ? Knowing that in the real
    > script the storit sub does a GET to a remote counter script which
    > generates a log file.
    >


    Yes. You need to make the parent process (which gets a TRUE value for
    pid) be the one that redirects to somewhere else (easily done using the
    CGI module). This seems to be what you are doing, since you call
    storit() unless $pid.

    What is going wrong for you? You do not report any errors or unwanted
    behaviour.

    DS
     
    David Squire, May 15, 2006
    #2
    1. Advertising

  3. Yohan N. Leder

    David Squire Guest

    David Squire wrote:
    > Yohan N. Leder wrote:
    >> Hi,
    >>
    >> Newbie in Perl and don't know how to do with this piece of script. I
    >> would like it redirects to mainboard.cgi without waiting for long
    >> forked process to be completed. Is it possible ? Knowing that in the
    >> real script the storit sub does a GET to a remote counter script which
    >> generates a log file.
    >>

    >
    > Yes. You need to make the parent process (which gets a TRUE value for
    > pid) be the one that redirects to somewhere else (easily done using the
    > CGI module). This seems to be what you are doing, since you call
    > storit() unless $pid.


    Premature posting, sorry. It's not as simple as that. See perdoc -f fork
    for issues such as zombie creation (if the parent does not wait for the
    child, which is what would happen in the scenario above), and connection
    of file system descriptors such as STDIN and STDOUT.

    Hmmm. Perhaps you could make the child be the one that redirects (since
    it should finish ASAP and can be waited for by the other), inheriting
    the socket connected IO, and reopen those in the parent. Just
    speculating though...

    DS
     
    David Squire, May 15, 2006
    #3
  4. A. Sinan Unur, May 15, 2006
    #4
  5. Yohan N. Leder

    Guest

    Yohan N. Leder <> wrote:
    > In article <Xns97C466AF9155asu1cornelledu@127.0.0.1>,
    > d says...
    > > Yohan N. Leder <> wrote in
    > > news::
    > >
    > > > Newbie in Perl and don't know how to do with this piece of script. I
    > > > would like it redirects to mainboard.cgi without waiting for long
    > > > forked process to be completed. Is it possible ?

    > >
    > > Yes.
    > >
    > > http://www.stonehenge.com/merlyn/LinuxMag/col39.html
    > >
    > > Sinan
    > >
    > >

    >
    > Thanks, but, unless error, my case doesn't fit what they talk about.
    > They say : "The CGI protocol is wonderful for the remote execution of
    > short tasks. But how do you execute a longer task? You can't just have
    > the task slowly executing without giving some kind of feedback to the
    > user, and eventually Apache will get bored and have the connection time
    > out anyway." And, in my case, I don't want to give feedbacl to the user
    > about forked process, but wish that the GET, proceeded in forked
    > process, doesn't block the HTML redirection written by the main process.


    HTML redirection is just another form of feedback. So it is true that
    that code can't just be naively copied and pasted, but it gives you
    everything you need to write your own code suited for your needs.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , May 15, 2006
    #5
  6. In article <Xns97C466AF9155asu1cornelledu@127.0.0.1>,
    d says...
    > Yohan N. Leder <> wrote in
    > news::
    >
    > > Newbie in Perl and don't know how to do with this piece of script. I
    > > would like it redirects to mainboard.cgi without waiting for long
    > > forked process to be completed. Is it possible ?

    >
    > Yes.
    >
    > http://www.stonehenge.com/merlyn/LinuxMag/col39.html
    >
    > Sinan
    >
    >


    Thanks, but, unless error, my case doesn't fit what they talk about.
    They say : "The CGI protocol is wonderful for the remote execution of
    short tasks. But how do you execute a longer task? You can't just have
    the task slowly executing without giving some kind of feedback to the
    user, and eventually Apache will get bored and have the connection time
    out anyway." And, in my case, I don't want to give feedbacl to the user
    about forked process, but wish that the GET, proceeded in forked
    process, doesn't block the HTML redirection written by the main process.
     
    Yohan N. Leder, May 15, 2006
    #6
  7. In article <e4a1fo$3d8$>,
    says...
    > David Squire wrote:
    > > Yohan N. Leder wrote:
    > >> Hi,
    > >>
    > >> Newbie in Perl and don't know how to do with this piece of script. I
    > >> would like it redirects to mainboard.cgi without waiting for long
    > >> forked process to be completed. Is it possible ? Knowing that in the
    > >> real script the storit sub does a GET to a remote counter script which
    > >> generates a log file.
    > >>

    > >
    > > Yes. You need to make the parent process (which gets a TRUE value for
    > > pid) be the one that redirects to somewhere else (easily done using the
    > > CGI module). This seems to be what you are doing, since you call
    > > storit() unless $pid.

    >
    > Premature posting, sorry. It's not as simple as that. See perdoc -f fork
    > for issues such as zombie creation (if the parent does not wait for the
    > child, which is what would happen in the scenario above), and connection
    > of file system descriptors such as STDIN and STDOUT.
    >
    > Hmmm. Perhaps you could make the child be the one that redirects (since
    > it should finish ASAP and can be waited for by the other), inheriting
    > the socket connected IO, and reopen those in the parent. Just
    > speculating though...
    >
    > DS
    >


    Well, in fact, the thing that does forked process my be long is that the
    remote cgi call (GET) by the storit sub can be busy or unavailable
    sometimes. Also, I would like that the main process doesn't take care at
    all of the forked one, and that the HTML redirection (Refresh) happens
    even if the GET didn't succeeded... But maybe it's unfaisable. Don't
    know.
     
    Yohan N. Leder, May 15, 2006
    #7
  8. Yohan N. Leder <> wrote in
    news::

    > In article <Xns97C466AF9155asu1cornelledu@127.0.0.1>,
    > d says...
    >> Yohan N. Leder <> wrote in
    >> news::
    >>
    >> > Newbie in Perl and don't know how to do with this piece of script.

    I
    >> > would like it redirects to mainboard.cgi without waiting for long
    >> > forked process to be completed. Is it possible ?

    >>
    >> Yes.
    >>
    >> http://www.stonehenge.com/merlyn/LinuxMag/col39.html


    >
    > Thanks, but, unless error, my case doesn't fit what they talk about.


    I don't think you have looked carefully at Randal's article to see that
    it shows you the proper way to fork a long running process.

    That is not the right kind of laziness.

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 15, 2006
    #8
  9. Yohan N. Leder wrote:
    >
    > [...] the thing that does forked process my be long is that the
    > remote cgi call (GET) by the storit sub can be busy or unavailable
    > sometimes. Also, I would like that the main process doesn't take care at
    > all of the forked one, and that the HTML redirection (Refresh) happens
    > even if the GET didn't succeeded... But maybe it's unfaisable. Don't
    > know.


    Maybe something like this could work for you:

    use strict;

    print "Content-Type: text/html\n";
    print "Location: index.html\n";
    print "\n";

    system ("/usr/local/bin/my-longproc &");

    -------------
    Notice the ampersand (&) at the end of the system command.
     
    Sumo Wrestler (or just ate too much), May 15, 2006
    #9
    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. RL
    Replies:
    2
    Views:
    605
  2. Zeljko

    Template ? Or whatever...

    Zeljko, Nov 21, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    314
    Saravana [MVP]
    Nov 22, 2003
  3. Scott M.
    Replies:
    2
    Views:
    533
    Kevin Spencer
    Feb 5, 2004
  4. Peter
    Replies:
    1
    Views:
    172
    -berlin.de
    Jul 19, 2007
  5. Peter
    Replies:
    1
    Views:
    109
    Brian McCauley
    Jul 20, 2007
Loading...

Share This Page