in reply to Multithreading for perl code
Try it this way (Note:untested):
#! perl -slw use strict; use threads; use threads::shared; use Thread::Queue; use Text::Fuzzy; our $semSTDIO :shared; sub tprint { lock $semSTDIO; print @_; } sub worker { my( $Q, $file1 ) = @_; my @array = split "\n", $file1; while( my $work = $Q->dequeue ) { my $tf = Text::Fuzzy->new( $work ); my $index = $tf->nearest( \@array ); tprint( "$work\t$array[ $index ]" ); } } our $T //= 4; ## -T=number of threads to use. our $F1 //= 'big.file'; ## -F1=big.file ## the 500,000 line fil +e our $F2 //= 'small.file'; ## -F2=small.file ## the 500 line file my $Q = new Thread::Queue; my $file1 = do{ local( @ARGV, $/ ) = $F1; <> }; my @threads = map threads->new( \&worker, $Q, $file1 ), 1 .. $T; $Q->enqueue( do{ local @ARGV = $F2; <> }, (undef) x $T ); $_->join for @threads;
Use as:thisScript -T=8 -F1=big.file -F2=small.file > results.file
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Multithreading for perl code
by locked_user sundialsvc4 (Abbot) on Apr 09, 2015 at 01:15 UTC |