Hi Mario,
Please find below a sample code for find with the issue.
# START
use warnings;
use Time::HiRes 'sleep';
use MCE;
use MCE::Flow;
use MCE::Queue;
use Data::Dumper;
use File::Basename;
sub gatherDataClosure
{
my $gatherRef = $_[0];
return sub {
my ($key, $value) = @_;
push (@{ $gatherRef->{FoundFiles}->{$key} }, $value);
return;
}
}
sub find
{
my ($args) = @_;
my @dirsToSearch = @_;
# Queues
my $dirQueue;
my $srchQueue;
my $sharedResrcMutex;
my @regexesToMatch;
my %results;
@regexesToMatch = defined($args->{regexData}) ?
((ref($args->{regexData}) eq "ARRAY") ? @{
$args->{regexData} } : ( $args->{regexData} )) :
();
if(!scalar(@dirsToSearch))
{
print "\nfind: Atleast one directory need to be specified for search\n";
exit 1;
}
if (!scalar(@regexesToMatch))
{
print "\nfind: Regex data is mandatory\n";
exit 1;
}
use constant {
MAX_PRODUCERS => 7,
MAX_CONSUMERS => 8,
};
$dirQueue = MCE::Queue->new(queue => \@dirsToSearch);
$srchQueue = MCE::Queue->new(fast => 1);
MCE::Flow::init {
task_end => sub {
my ($mce, $task_id, $task_name) = @_;
$srchQueue->enqueue((undef) x MAX_CONSUMERS)
if $task_name eq 'prod';
},
gather => gatherDataClosure(\%results)
};
## Override any MCE options and run. Notice how max_workers and
## task_name take an anonymous array to configure both tasks.
mce_flow {
max_workers => [ MAX_PRODUCERS, MAX_CONSUMERS ],
task_name => [ 'prod', 'cons' ],
},
sub {
## Prod Task. Allow time for wid 1 to enqueue any dir entries.
## Otherwise, workers (wid 2+) may terminate early.
sleep 0.1 if MCE->task_wid > 1;
while (defined (my $dir = $dirQueue->dequeue_nb))
{
my (@files, @dirs);
foreach (glob("$dir/*"))
{
if (-d $_)
{
push @dirs, $_;
next;
}
push @files, $_;
}
if (scalar @dirs)
{
$dirQueue->enqueue(@dirs);
$srchQueue->enqueue(@dirs);
}
$srchQueue->enqueue(@files) if scalar @files;
}
},
sub {
## Cons Task.
while (defined (my $node = $srchQueue->dequeue)) {
foreach (@regexesToMatch)
{
MCE->gather($_, $node) if ($node =~ qr/$_/i);
}
}
};
## Workers persist in models. This may be ommitted. It will run
## automatically during exiting if not already called.
MCE::Flow::finish;
return %results;
}
my $regex = qr/[a-zA-Z0-9]+\.txt$/;
my %srchResults = find({regexData => $regex}, "C:/temp");
print Dumper(\%srchResults);
#END
Thanks
Rissen
On Thu, Oct 22, 2015 at 9:32 AM Mario Roy via RT <bug-MCE@rt.cpan.org>
wrote:
Show quoted text> <URL:
https://rt.cpan.org/Ticket/Display.html?id=107925 >
>
> Hello Rissen,
>
> Can you provide a small script so that I can reproduce the issue. MCE 1.7
> will be released soon and want to include the fix if feasible.
>
> Thanks.
>
> Kind regards,
> Mario
>
>