Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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?

Replies are listed 'Best First'.
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 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 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 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 Anonymous Monk on Sep 30, 2009 at 04:46 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://798189]
Approved by AnomalousMonk
[Lady_Aleena]: I hope you have a wonderful day Discipulus. 8)
[Corion]: A good morning to you too ;)
[choroba]: Good morning, Monks!
[Corion]: And a good daypart to you as well, Lady_Aleena !
[robby_dobby]: Hey monkeys, do you ever get the realization that you're all waking up to chaos? I suppose not :P
[Lady_Aleena]: Good morning Corion, I hope you have a wonderful day as well. 8)
[Corion]: Yesterday I encountered an interesting data structure problem. I have a remote program that emits events, and my client listens for these events with one-shot callbacks, that is, I register the callback and if the event gets generated that callback ...
[Lady_Aleena]: robby_dobby, every day. Chaos is my life with few controls.
[Corion]: ... gets called once. The data structure for that is just a hash of arrays, mapping the event type to a queue of registered one-shots, and the first one-shot from the queue gets removed and called.
[Corion]: But now I want to register a one-shot for two events, of which only one will arrive, so my data structure doesn't work anymore...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2017-05-29 07:50 GMT
Find Nodes?
    Voting Booth?