How do I manage multiple subprocesses in Perl?

perl

Solution

Don't use threads. Threads suck. The proper way is to `fork` multiple processes and `wait` for them to finish. If you use `wait` or `waitpid`, the exit status of the process in question will be available in `$?`.

See the perldocs for fork, wait, and waitpid, and also the examples in this SO thread.

If all you need is to just manage a pool of subprocesses that doesn't exceed a certain size, check out the excellent Parallel::ForkManager.

Problem

I have a Perl program that needs to run about half a dozen programs at the same time in the background and wait for them all to finish before continuing. It is also very important that the exit status of each can be captured. Is there a common idiom for doing this in Perl? I'm currently thinking of using threads.

Original source

Related problems