-
-
Save msaboff/8540538562a411f3b2b6b9029aa23cb2 to your computer and use it in GitHub Desktop.
Update to 1.pm using IO::Select
This file contains 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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use 5.8.8; | |
use IO::Handle; | |
use IO::Select; | |
use FindBin; | |
use Config; | |
my $Bin; | |
BEGIN { | |
$ENV{DBIC_OVERWRITE_HELPER_METHODS_OK} = 1; | |
unshift @INC, "$FindBin::Bin"; | |
unshift @INC, "$FindBin::Bin/lib"; | |
unshift @INC, "$FindBin::Bin/local/lib/perl5"; | |
unshift @INC, "$FindBin::Bin/local/lib/perl5/$Config{archname}"; | |
$ENV{LOAD_ROUTES} = 1; | |
} | |
use Parallel::ForkManager; | |
use Socket; | |
my $processes = 4; | |
my $timeout = 31; | |
my $pm = Parallel::ForkManager->new($processes); | |
my @files = ('a'..'z'); | |
sub getFile { | |
return shift @files; | |
} | |
my @children; | |
my @parents; | |
my $activeChildren = 0; | |
my $select = IO::Select->new(); | |
# Open each process | |
PROCESSES: | |
foreach my $i (0..$processes-1) { | |
socketpair($children[$i], $parents[$i], AF_UNIX, SOCK_STREAM, PF_UNSPEC); | |
my $child = $children[$i]; | |
my $parent = $parents[$i]; | |
$child->autoflush(1); | |
$parent->autoflush(1); | |
my $pid = $pm->start; | |
if ($pid) { # parent | |
$select->add($child); | |
# each child starts with a file; | |
my $file = shift @files; | |
chomp $file; | |
if ($file) { | |
print $child "$file\n"; | |
$activeChildren++; | |
} | |
next PROCESSES; | |
} | |
# children will start here | |
while (<$parent>) { | |
my $file = $_; | |
chomp $file; | |
if ($file eq 'END') { | |
last; | |
} | |
sleep(int(rand(4) + 0.5)); # Do work in child here | |
print "child $i processed $file\n"; | |
print $parent "Ready\n"; # Send child's result | |
} | |
$child->close(); | |
$pm->finish; | |
exit; | |
} | |
my @ready; | |
FILES: | |
while ($activeChildren and @ready = $select->can_read($timeout)) { | |
foreach (@ready) { | |
my $readyChild = $_; | |
# print "Child $readyChild is ready\n"; | |
my $childMsg = <$readyChild>; | |
chomp $childMsg; | |
# Handle child's result | |
$activeChildren--; | |
my $file = shift @files; | |
if ($file) { | |
chomp $file; | |
print $readyChild "$file\n"; | |
$activeChildren++; | |
} elsif (!$activeChildren) { | |
last FILES; | |
} | |
} | |
# print "Waiting for CR\n"; | |
} | |
print "Shutting down children\n"; | |
foreach (@children) { | |
print $_ "END\n"; | |
} | |
foreach (@parents) { | |
$_->close(); | |
} | |
$pm->wait_all_children; | |
print "Parent exiting\n"; | |
print "done\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment