in reply to Re: Thread Design help
in thread Thread Design help
use strict; use threads; use Data::Dumper; use Thread::Queue; my $THREADS = 5; my %dataEntity; while(<>){ chomp; next if !length($_); my ($dsName,$passwd) = split /\|/, $_; $dataEntity{$dsName} = $passwd; } my $request = Thread::Queue->new; my $response = Thread::Queue->new; # Submit all requests for my $dbname (keys %dataEntity) { $request->enqueue([$dbname,$dataEntity{$dbname}]); }; # Tell each thread that we're done for (1..$THREADS) { $request->enqueue(undef); }; # Launch our threads for (1..$THREADS) { async(\&getData); }; sub getData { while (my $job = $request->dequeue()) { my ($dbname, $credentials) = @$job; #connect to DB, retrieve information my $dbh = getConn($dbname,$credentials); my %results; my $resArrRef = $dbh->selectall_arrayref("select srvname,dbnam +e from syslogins",{ Slice => {} }); foreach my $row ( @$resArrRef ) { $results{$row->{srvname}} = $row->{dbname}; } $response->enqueue(\%results); }; # tell our main thread we're done $response->enqueue( undef ); }; while (my $payload = $response->dequeue or $THREADS--) { print Dumper $payload; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Thread Design help
by Corion (Patriarch) on Sep 08, 2010 at 17:24 UTC | |
by Anonymous Monk on Sep 08, 2010 at 17:44 UTC | |
by Corion (Patriarch) on Sep 08, 2010 at 17:57 UTC | |
by perlCrazy (Monk) on Sep 08, 2010 at 18:18 UTC | |
by BrowserUk (Patriarch) on Sep 08, 2010 at 19:59 UTC | |
| |
by Corion (Patriarch) on Sep 08, 2010 at 18:20 UTC | |
by perlCrazy (Monk) on Sep 08, 2010 at 18:06 UTC | |
|
Re^3: Thread Design help
by perlCrazy (Monk) on Sep 11, 2010 at 06:46 UTC | |
by Corion (Patriarch) on Sep 11, 2010 at 08:26 UTC |