This should work anywhere including cygwin:
use threads; use Threads::Queue; my $Q = new Thread::Queue; my $thread = async { ## create/open DB here my $dbs = ...; while( my $query = $Q->dequeue ) { $dbs->query( $query ); } ## finish with DB here }; while (<I>) { my $line = $_; my @bucket = $bucket->based_on($line); # Data::Bucket my @acct_vals; for (@bucket) { # %hash = calculate based on input and bucket value push @acct_vals, \%hash; } next unless @acct_vals; @acct_vals = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, $_->{dist} ] } @acct_vals; my @dump ; for my $acct_val (@acct_vals) { last if $acct_val->{dist} > $match_threshold * 1.5 ; push @dump, $acct_val; } my $d = Data::Dumper->new([\@dump], ['dump']) ; $d->Purity(1)->Terse(1)->Deepcopy(1); ## Build sql and post to db thread (Could be done better) my $args = "'" . join( "','", $acct_name, $clean_acct_name, $d->Du +mp) . "'"; sleep 1 while $Q->pending > 10; ## Adjust threshhold to suit. $Q-enqueue( sprintf 'INSERT INTO addresses VALUES (%s)', $args ); unless (++$counter % $report_very) { warn "$counter in $now"; # Time::Lapse by Scott Godin } } $Q->enqueue( undef ); ## Terminate db thread $thread->join; ## And wait for it to finish.
In reply to Re: Taking advantage of dual processors (threads)
by BrowserUk
in thread Taking advantage of dual processors [tag://parallel,cygwin,multi-core,fork,i/o]
by metaperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |