Last active
December 14, 2015 18:28
-
-
Save skaji/5129180 to your computer and use it in GitHub Desktop.
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 Pod::Usage 'pod2usage'; | |
use Getopt::Long qw( | |
:config | |
posix_default no_ignore_case | |
bundling auto_help | |
); | |
GetOptions( | |
'parallel|P=i' => \(my $parallel = 3), | |
) or pod2usage(1); | |
pod2usage(1) if !@ARGV; | |
my $children = children_fork($parallel, \@ARGV); | |
my $i = 0; | |
while (<STDIN>) { | |
my $child = $children->[$i]; | |
print { $child->{stdin} } $_ | |
or die "Can't print (pid=$child->{pid}): $!"; | |
$i = ($i + 1) % $parallel; | |
} | |
for my $child (@$children) { | |
close $child->{stdin} | |
or die "Can't close (pid=$child->{pid}): $!"; | |
waitpid $child->{pid}, 0; | |
} | |
sub children_fork { | |
my ($parallel, $command) = @_; | |
my @children; | |
for (1..$parallel) { | |
my $pid = open my $stdin, '|-', @$command | |
or die "Can't fork '@$command': $!"; | |
push @children, { | |
pid => $pid, | |
stdin => $stdin, | |
}; | |
} | |
return \@children; | |
} | |
__END__ | |
=head1 SYNOPSIS | |
cat joblist.txt | ./round_robin_stdin.pl --parallel 5 -- somecommand | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment