Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: CGI script calling a perl script

by mbethke (Hermit)
on Apr 24, 2013 at 19:53 UTC ( [id://1030501]=note: print w/replies, xml ) Need Help??


in reply to CGI script calling a perl script

I guess the HTML isn't being sent out because it's still sitting in a buffer somewhere and the main CGI process can't exit and flush the buffer as long as there is still a child process active. Proc::Daemon could do what you want but I don't think that's a particularly good architecture to start with. If myscript.pl takes a long time to run (which is the only reason I can imagine for not doing its job on the main task), you could easily be DOSed by calling the page with step=0 repeatedly. Have you thought about using AJAX? You could load the main page and have it call myscript in the background as a CGI of its own, which would then print its results in JSON or something as they are produced, to be consumed by the JavaScript on your page.

Replies are listed 'Best First'.
Re^2: CGI script calling a perl script
by Anonymous Monk on Apr 24, 2013 at 20:02 UTC
    Yes myscript.pl takes a long time to run. I'm not worried about DOS because of various other security mechanisms in place. Unfortunately I don't know AJAX so I'm stuck trying to with CGI and perl. How would Proc::Daemon help in this case? Would it replace Proc::Background?

      Sort of, though the API is different. It helps you completely detach a process from its parent so the parent can exit while the child continues. I guess the child will write some file to the file system than and the parent somehow finds out when it's done?

      If you don't mind forcing users to enable scripting on your page, the whole AJAX stuff isn't all that difficult. Wikipedia on AJAJ has a very small example using jQuery (you could call that function that does $.getJSON in an onLoad handler or so), and the article on XMLHttpRequest has some resources on how to do it without jQuery.

Re^2: CGI script calling a perl script
by Anonymous Monk on Apr 24, 2013 at 20:15 UTC
    Is there a way to force flush the buffer?
      I'm not entirely sure about CGI but as it uses STDOUT underneath a
      use IO::Handle; STDOUT->autoflush(1);
      should at least get the HTML out to the web server. It won't solve the problem of the CGI script not exiting and thus not sending an EOF to the browser though, so the browser will keep showing "page loading". Maybe you can close STDOUT before exiting?
        Unfortunately the flush didn't change anything. Closing STDOUT didn't help either. Seems like a simple thing I need to do - call a perl script, let it run in the background and continue serving HTML.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1030501]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-06-15 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.