如何在 Perl 中管理多个子进程? [英] How do I manage multiple subprocesses in Perl?

查看:63
本文介绍了如何在 Perl 中管理多个子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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.

在 Perl 中这样做是否有一个常见的习惯用法?我目前正在考虑使用线程.

Is there a common idiom for doing this in Perl? I'm currently thinking of using threads.

推荐答案

不要使用线程.线程很烂.正确的方法是fork多个进程并wait让它们完成.如果您使用 waitwaitpid,则相关进程的退出状态将在 $? 中可用.

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 $?.

请参阅 fork 的 perldoc,等待waitpid,以及这个 SO 线程.

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

如果您只需要管理一个不超过特定大小的子进程池,请查看优秀的 Parallel::ForkManager.

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.

这篇关于如何在 Perl 中管理多个子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆