开发者

How do I manage multiple subprocesses in Perl?

I have a Perl program that needs to run about half a dozen programs at the same time in t开发者_开发百科he 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜