如何在Perl中管理派生池? [英] How can I manage a fork pool in Perl?

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

问题描述

我正在设置要通过SSH分发到批次"中的多台服务器的功能.我基本上想一次维护5个连接,而当一个连接完成时则打开另一个连接(遵循一系列服务器IP).

I'm setting something up to SSH out to several servers in 'batches'. I basically want to maintain 5 connections at a time, and when one finishes open up another (following an array of server IPs).

我想知道是否应该使用 fork() ?如果是这样,我可以使用什么逻辑来确保我一次养育5个孩子?

I'm wondering for something like this should I be using fork()? If so, what logic can I use to ensure that the I maintain 5 children at a time?

推荐答案

分叉(或线程化)是您想要的,但是您应该查看

Forking (or threading) is what you want, but you should look at CPAN for modules that will provide most of what you need to prevent you from reinventing the wheel and going through the learning pains of what you need to do.

例如, Parallel :: ForkManager 看起来正是您想要的.

For example, Parallel::ForkManager looks like it's EXACTLY what you want.

use Parallel::ForkManager;

$pm = new Parallel::ForkManager($MAX_PROCESSES);

foreach $data (@all_data) {
  # Forks and returns the pid for the child:
  my $pid = $pm->start and next; 

  ... do some work with $data in the child process ...

  $pm->finish; # Terminates the child process
}

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

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