Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Execute as new process?

by perlpreben (Beadle)
on Sep 29, 2009 at 20:33 UTC ( #798189=perlquestion: print w/ replies, xml ) Need Help??
perlpreben has asked for the wisdom of the Perl Monks concerning the following question:

I want a webpage(cgi script) to execute a perl script , but continue loading the webpage. Now it waits until the program is done, THEN continues.

I have read documentation on exec, system, backticks, and tried numerous examples but can make it work.

Its running on a unix system, so I also tried appending & at the end of the command, but that doesnt work either

Any clue?

Comment on Execute as new process?
Re: Execute as new process?
by Utilitarian (Vicar) on Sep 29, 2009 at 20:40 UTC
    I think you should look at making the Perl produced content of the page load via AJAX
Re: Execute as new process?
by Illuminatus (Curate) on Sep 29, 2009 at 20:49 UTC
    I think you need to provide an example. In general, if you use system with a command that ends with a '&', the call should immediately return.
Re: Execute as new process?
by jakobi (Pilgrim) on Sep 29, 2009 at 20:50 UTC

    lower level approaches:

    system and qx// are synchronous and make perl wait until the syscall in question is done. You could try system("cmd&") to have the shell run the command and return to perl w/o waiting.

    An interesting question now is whether you want $? or the output of the command before finishing to send the page to the client. If that's the case: check out e.g. waitpid, perlipc and maybe also use something like the following sketch

    if (not fork()) {system("cmd >cmd-out"); ... exit} ...additionalprocessing-in-parallel...; wait; # or a more specific waitpid...; # if you've a lot of forks and/or run a long time, # consider to reap your children: wait, SIG{CHLD} # -> man perlipc open(FH,"<cmd-out");... ...process the results of cmd and add them to the page... # (or consider a state file/sessions, if the background # command can and should run for more than the duration # of a single client request) finish the page
Re: Execute as new process?
by Joost (Canon) on Sep 29, 2009 at 21:18 UTC
Re: Execute as new process?
by corenth (Monk) on Sep 29, 2009 at 21:53 UTC
    A long time ago, I played with POE (Perl Object Environment) to good effect. I don't recall much about it, and it may not be what you want, but it should give you the effect you're looking for.
Re: Execute as new process?
by Anonymous Monk on Sep 30, 2009 at 04:46 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://798189]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-09-19 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (135 votes), past polls