in reply to Multi-thread combining the results together
Hi Marshall,
Inbound two MCE examples in response to monks Discipulus and 1nickt suggesting MCE. The 1st example stores to a hash.
use strict; use warnings; use MCE::Loop; use Data::Dumper; my @tokens = qw( ab bc cd de ef fg gh ); sub build_regex { my ($token) = @_; chop $token; } MCE::Loop->init( chunk_size => 1, max_workers => 4, ); my %result = mce_loop { my $token = $_; my $regex = build_regex($token); my @line_results = grep { $_ ne $token and /$regex/ } @tokens; MCE->gather( $token => \@line_results ) if @line_results; } @tokens; MCE::Loop->finish; print Dumper(\%result), "\n"; __END__ $VAR1 = { 'ef' => [ 'fg' ], 'fg' => [ 'gh' ], 'bc' => [ 'cd' ], 'ab' => [ 'bc' ], 'cd' => [ 'de' ], 'de' => [ 'ef' ] };
The 2nd example stores to an array and maintains order, possible with MCE::Relay which runs orderly.
use strict; use warnings; use MCE::Loop; use Data::Dumper; my @tokens = qw( ab bc cd de ef fg gh ); sub build_regex { my ($token) = @_; chop $token; } MCE::Loop->init( chunk_size => 1, max_workers => 4, init_relay => 1, ); my @result = mce_loop { my $token = $_; my $regex = build_regex($token); my @line_results = grep { $_ ne $token and /$regex/ } @tokens; MCE::relay { MCE->gather(\@line_results) if @line_results; }; } @tokens; MCE::Loop->finish; print Dumper(\@result), "\n"; __END__ $VAR1 = [ [ 'bc' ], [ 'cd' ], [ 'de' ], [ 'ef' ], [ 'fg' ], [ 'gh' ] ];
Regards, Mario
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Multi-thread combining the results together
by marioroy (Prior) on Jul 25, 2019 at 12:18 UTC |