Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I manage multiple subprocesses in Perl?

Tags:

perl

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.

like image 559
AndrewR Avatar asked Feb 24 '10 00:02

AndrewR


2 Answers

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.

like image 116
friedo Avatar answered Sep 28 '22 08:09

friedo


Normally you would fork + exec (on unix based systems, this is traditional)

The fork call will duplicate the current process, and if you needed to you could then call exec in one of the children to do something different. It sounds like you just want to fork and call a different method/function in your child process.

If you want something more complex, check cpan for POE - that lets you manage all sorts of complex scenarios.

Useful links:

  • "spawning multiple child processes" on PerlMonks

Google "perl cookbook forking server" too - only allowed to post one link unless I log in.

like image 37
Richard Huxton Avatar answered Sep 28 '22 08:09

Richard Huxton