Hello,
I came across a cool post by Abigail-II and thought to try it with MCE::Hobo. Thank you, trippledubs for posting the link.
On Windows, MCE::Hobo spawns threads. Otherwise, childrens on Cygwin and other platforms. The following is a demonstration for many hobos, but never more than a fixed number at a given time.
use strict;
use warnings;
use MCE::Hobo;
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Based on http://www.perlmonks.org/?node_id=175715 ( by Abigail-II ).
# Currently, MCE::Hobo emits a message to STDERR if unable to spawn.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sub mhobo ($$&) {
my ($count, $max, $code) = @_;
foreach my $c (1 .. $count) {
MCE::Hobo->waitone() unless $c <= $max;
exit(255) unless defined (my $h = MCE::Hobo->create($code, $c));
}
MCE::Hobo->waitall();
}
sub ahobo (\@$&) {
my ($data, $max, $code) = @_;
my $c = 0;
foreach my $data (@$data) {
MCE::Hobo->waitone() unless ++$c <= $max;
exit(255) unless defined (my $h = MCE::Hobo->create($code, $data))
+;
}
MCE::Hobo->waitall();
}
STDOUT->autoflush(1); # Perl 5.14 or higher
mhobo 9, 3, sub {
print $_[0]."\n";
for (1 .. 4e7) { 1 } # simulate busy
};
my @input = ( 'a' .. 'i' );
ahobo @input, 3, sub {
print $_[0]."\n";
for (1 .. 4e7) { 1 } # ditto
};
Regards, Mario.