Skip to content

Instantly share code, notes, and snippets.

@skaji
Last active December 14, 2015 18:28
Show Gist options
  • Save skaji/5129180 to your computer and use it in GitHub Desktop.
Save skaji/5129180 to your computer and use it in GitHub Desktop.
#!/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