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"

In reply to Re: Sharing DBI between threads by BrowserUk
in thread Sharing DBI between threads by bagent

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.