in reply to Sharing DBI between threads

Hm. Even ignoring simple errors ( naming the passed $sth as $db_source, and then doing nothing with it), I'm not at all sure than your sample code makes any sense at all.

There are more fundemental coding issues: like you finish the sth and close the connection as soon as you've started the threads, which inevitably is going to be before they've had a chance to make much use of them. If it was ever going to work, you'd have to do that after you've joined the threads.

What do you expect to happen when you retrieve the results from a statement handle on multiple threads?

Assuming for a moment that DBI and MySQL had no problems with you calling from multiple threads, then there are two possibilities:

Finally, if your concern is that populating the queue with all the returned data will consume to much memory, don't do it all at once. Instead, feed the queue slowly:

use DBI; use threads; use Thead::Queue; my $Q = new Thread::Queue; my $dbh = DBI->connect(...); my $sth = $dbh->prepare("SELECT ..."); $sth->execute(); $thr1 = threads->new(\&some_sub, $Q ); $thr2 = threads->new(\&some_sub, $Q ); while( my $ref = $sth->fetchrow_hashref() ) { $Q->enqueue( join $;, %$ref ); sleep 1 while $Q->pending > 10; } $Q->enqueue( (undef) x 2 ); $thr1->join; $thr2-join; $sth->finish(); $dbh->disconnect(); sub some_sub { my ( $Q ) = @_; while( my %row = split $;, $Q->dequeue ) { #... } }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^2: Sharing DBI between threads
by bagent (Novice) on Jan 21, 2010 at 22:44 UTC
    BrowserUk, thank you, really helpful advice about checking $Q->pending, I think I will use this.