Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^2: Doing every X seconds

by mr_mischief (Monsignor)
on Aug 06, 2018 at 23:00 UTC ( #1219970=note: print w/replies, xml ) Need Help??

in reply to Re: Doing every X seconds
in thread Doing every X seconds

You forgot to check the definedness of $child, as upon failure to fork you'll get undef. You likely do not want to pretend you're supposed to be executing the child case code in that circumstance.

I'm puzzled by "# child must explicitly exit otherwise both would continue executing this". The wording suggests both parent and child will continue executing the child case which of course is untrue. As written, the child would fall out of the else clause and the parent's waitpid would still see the child's exit status if that call to exit were not present. You then say to "# reach here if job processing completed successfully" which in your case only the parent will and the child won't because of the above-mentioned exit. Perhaps it would be clearer for the first one to say "... or the child will continue past the else block and we only want the parent to do so" or something similar.

If the intent is for both programs to be done after the branch, a shared exit just after the entire if statement could be useful. Any code you intend for the parent only to run after the child is gone could be placed in the parent's case in the if block, after the loop on waitpid. It's also possible to have the parent exit and the child continue, and that's desirable under certain circumstances.

At this point I think it's worth mentioning there are tools to do this handling for you. Depending on your exact needs, there are Parallel::ForkManager, AnyEvent::ForkManager, IPC::Run, IPC::Run3, Proc::Fork, Proc::Fork::Control, Proc::Daemon, and a whole lot more.

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2020-09-29 05:16 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (146 votes). Check out past polls.