Created
January 11, 2012 19:37
-
-
Save riywo/1596357 to your computer and use it in GitHub Desktop.
Parallel::ForkManagerでpipe使って簡単に結果渡し
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Parallel::ForkManager; | |
use IO::Pipe; | |
use JSON; | |
my @hosts = map { sprintf("host%02d", $_) } (1..10); | |
my $workers = 5; | |
my $pm = Parallel::ForkManager->new($workers); | |
my @pipes = map { IO::Pipe->new } (1..$workers); | |
my $job_id = -1; | |
for my $host (@hosts) { | |
$job_id++; | |
my $pid = $pm->start and next; | |
my $pipe_id = $job_id % $workers; | |
my $pipe = $pipes[$pipe_id]->writer; | |
my $ret = run($host); | |
my $json = encode_json({ | |
'host' => $host, | |
'return' => $ret, | |
'pipe' => $pipe_id, | |
}); | |
pprint($pipe, $json); | |
$pm->finish; | |
} | |
$pm->wait_all_children; | |
for my $pipe (@pipes) { | |
$pipe->reader; | |
while (<$pipe>) { | |
chomp; | |
my $data = decode_json($_); | |
print "[$data->{'host'}] $data->{'return'}"; | |
} | |
} | |
sub pprint { | |
my ($pipe, $str) = @_; | |
print $str."\n"; | |
print {$pipe} $str."\n"; | |
} | |
#------------------------------------------------- | |
sub run { | |
my $host = shift; | |
sleep 1; | |
return "ssh return of $host\n"; | |
} | |
__END__ | |
{"pipe":0,"return":"ssh return of host01\n","host":"host01"} | |
{"pipe":2,"return":"ssh return of host03\n","host":"host03"} | |
{"pipe":1,"return":"ssh return of host02\n","host":"host02"} | |
{"pipe":4,"return":"ssh return of host05\n","host":"host05"} | |
{"pipe":3,"return":"ssh return of host04\n","host":"host04"} | |
{"pipe":0,"return":"ssh return of host06\n","host":"host06"} | |
{"pipe":1,"return":"ssh return of host07\n","host":"host07"} | |
{"pipe":2,"return":"ssh return of host08\n","host":"host08"} | |
{"pipe":4,"return":"ssh return of host10\n","host":"host10"} | |
{"pipe":3,"return":"ssh return of host09\n","host":"host09"} | |
[host01] ssh return of host01 | |
[host06] ssh return of host06 | |
[host02] ssh return of host02 | |
[host07] ssh return of host07 | |
[host03] ssh return of host03 | |
[host08] ssh return of host08 | |
[host04] ssh return of host04 | |
[host09] ssh return of host09 | |
[host05] ssh return of host05 | |
[host10] ssh return of host10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment