The channel object is bidirectional. Therefore, the worker can send its PID value to the producer. This has the effect of max one item in the channel. The producer writes to STDOUT like the demonstration by karlgoethebier.
#!/usr/bin/env perl
use strict;
use warnings;
use MCE::Hobo;
use MCE::Channel;
use feature qw(say);
use constant AMOUNT => 0.001;
my $cores = MCE::Util::get_ncpu();
my $queue = MCE::Channel->new();
MCE::Hobo->init(
void_context => 1,
posix_exit => 1,
);
# Consumers
sub consumer_task {
while ( my $input = $queue->recv() ) {
my $id = $input->{id};
$queue->send2( MCE::Hobo->pid() );
qx( sleep @{[ AMOUNT ]};)
}
}
MCE::Hobo->create( \&consumer_task ) for 1..$cores;
# Producer
for my $id ( 1..800 ) {
$queue->send({ id => $id });
my $hobo_pid = $queue->recv2();
say qq($id ) . $hobo_pid;
}
$queue->end();
MCE::Hobo->wait_all();