Long running CGI script

Discussion in 'Perl Misc' started by Stephen O'D, Jun 17, 2005.

  1. Stephen O'D

    Stephen O'D Guest

    I have a script that asks for some input on several screens, and at the
    end it kicks off a connection to a database which does some work that
    could potentially take a long time. I dont want to hold up displaying
    the webpage while this is running - I would prefer to print a message
    to the browser saying the job is running in the background. In other
    works, I want to allow the script to continue running, but notify the
    browser that I have finished talking to it.

    I found a script written by Randal Schwartz that says todo this all you
    have todo is close STDIN and STDOUT:-

    open STDIN, "</dev/null";
    open STDOUT, ">/dev/null";

    However when I do this the script seem to die (although no die message
    gets into the error logs - it just seems to give up silently). Also, a
    system command to copy a file gives an error "Not A Typewriter". If I
    remove the system command, the database connection does not work
    correctly (it seems to work sometimes though??!!).

    How can I 'disconnnect' the browser let my script continue todo some
    work? I would prefer not to fork.

    Thanks,

    Stephen.
    Stephen O'D, Jun 17, 2005
    #1
    1. Advertising

  2. Stephen O'D

    Larry Guest

    I always thought you had to "fork" to do that. In addition you need to
    close STDOUT (as you've done) before you fork.

    As for the dying, you need to find out where it's hapenning. You
    mention that you have a log file. Make sure you do $| = 1 after
    opening the log file, and just print progress messages to the log file
    every few lines in your code. Eventually you'll find the exact place
    where it's dying, and you'll be able to debug it better.
    Larry, Jun 17, 2005
    #2
    1. Advertising

  3. Stephen O'D

    Guest

    "Stephen O'D" <> wrote:
    > I have a script that asks for some input on several screens, and at the
    > end it kicks off a connection to a database which does some work that
    > could potentially take a long time. I dont want to hold up displaying
    > the webpage while this is running - I would prefer to print a message
    > to the browser saying the job is running in the background. In other
    > works, I want to allow the script to continue running, but notify the
    > browser that I have finished talking to it.
    >
    > I found a script written by Randal Schwartz that says todo this all you
    > have todo is close STDIN and STDOUT:-
    >
    > open STDIN, "</dev/null";
    > open STDOUT, ">/dev/null";
    >
    > However when I do this the script seem to die (although no die message
    > gets into the error logs - it just seems to give up silently).


    Apache, and probably other web servers, will kill the CGI process once
    the process closes STDOUT and STDERR. That is probably why your script
    is dying. Either reconfigure the server not to do this (I don't know how
    to do that) or fork a new process to do the heavy lifting so that the
    original process can go away gracefully.


    > Also, a
    > system command to copy a file gives an error "Not A Typewriter". If I
    > remove the system command, the database connection does not work
    > correctly (it seems to work sometimes though??!!).


    Sorry, I can't make sense of this. Can you show the relevant code?


    > How can I 'disconnnect' the browser let my script continue todo some
    > work? I would prefer not to fork.


    I'd prefer to stop aging, but that isn't working out for me, either :)

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jun 17, 2005
    #3
  4. Stephen O'D

    Guest

    wrote:
    > "Stephen O'D" <> wrote:
    > > I have a script that asks for some input on several screens, and at the
    > > end it kicks off a connection to a database which does some work that
    > > could potentially take a long time. I dont want to hold up displaying
    > > the webpage while this is running - I would prefer to print a message
    > > to the browser saying the job is running in the background. In other
    > > works, I want to allow the script to continue running, but notify the
    > > browser that I have finished talking to it.
    > >


    Are you on a win32 machine? If so, Why can't you just code

    print "this may take awile...";
    system("myscript.pl");

    I'm not really a Unix Perl guy, but "shell()" sounds familiar.

    [---]

    > Apache, and probably other web servers, will kill the CGI process once
    > the process closes STDOUT and STDERR. That is probably why your script
    > is dying. Either reconfigure the server not to do this (I don't know how
    > to do that) or fork a new process to do the heavy lifting so that the
    > original process can go away gracefully.
    >
    >

    [---]

    Try here:
    http://perldoc.perl.org/File/Copy.html

    >
    > > How can I 'disconnnect' the browser let my script continue todo some
    > > work? I would prefer not to fork.


    Try looking at this page;
    http://perldoc.perl.org/functions/exec.html

    Exec() and system() seem appropos to your needs- but maybe I'm
    misreading you.

    If what you need is a formal "callback" functionality, you might be
    best served by using semaphores- as Perl isn't really an "event-driven"
    infrastructure.


    kDot
    , Jun 18, 2005
    #4
    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. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    672
    Eric Sosman
    Jul 8, 2003
  2. Benjamin Rutt
    Replies:
    2
    Views:
    930
    Peter Hansen
    Dec 20, 2005
  3. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,184
    Peter Shaggy Haywood
    Sep 20, 2005
  4. Replies:
    3
    Views:
    116
  5. kath
    Replies:
    4
    Views:
    562
    J. Gleixner
    Apr 9, 2007
Loading...

Share This Page