Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Threads Enqueue Hashes

by NewtonMan (Initiate)
on Aug 14, 2012 at 17:01 UTC ( [id://987415]=perlquestion: print w/replies, xml ) Need Help??

NewtonMan has asked for the wisdom of the Perl Monks concerning the following question:

Hi There, Can someone tell me why this is not enqueueing the hashs, and tell me the solution? Here is the code:
#!/usr/bin/perl use warnings; use threads qw[ yield ]; use threads::shared; use Thread::Queue; use Mysql; use constant NTHREADS => $ARGV[1]; $db = Mysql->connect('localhost', 'xxx', 'xxx', 'xxx'); %action = GetActionDetail($ARGV[0]); %userData = GetUserDetail($action{'id_user'}); %companyData = GetCompanyDetail($userData{'id_company'}); my $pos :shared = 0; $sql = "SELECT * FROM v_queue WHERE id=".$ARGV[0]; $query = $db->query($sql); $total = $query->numrows; if ($total>0){ my $Q = Thread::Queue->new; my @threads = map threads->create( \&worker, $Q ), 1 .. NTHREADS; sleep 0.5 while $Q->pending; while (%queueData = $query->fetchhash){ my %item = (); $item{'action'} = $action; $item{'user'} = $userData; $item{'company'} = $companyData; $item{'queueData'} = $queueData; $Q->enqueue( $item ); lock $pos; } $Q->enqueue( (undef) x NTHREADS ); $_->join for @threads; } sub GetActionDetail { my $id = $_[0]; my $sql = "SELECT * FROM `actions` WHERE id='$id'"; my $query = $db->query($sql); my $found = $query->numrows; if ($found==1){ return $query->fetchhash; } else { return "false"; } } sub GetUserDetail { my $id = $_[0]; my $sql = "SELECT * FROM `user` WHERE id='$id'"; my $query = $db->query($sql); my $found = $query->numrows; if ($found==1){ return $query->fetchhash; } else { return "false"; } } sub GetCompanyDetail { my $id = $_[0]; my $sql = "SELECT * FROM `company` WHERE id='$id'"; my $query = $db->query($sql); my $found = $query->numrows; if ($found==1){ return $query->fetchhash; } else { return "false"; } } sub worker { my $Q = shift; my $tid = threads->tid; while( $item = $Q->dequeue ) { print "ITEM ID: ".$item{'action'}{'id'}."\n"; } }

Replies are listed 'Best First'.
Re: Threads Enqueue Hashes
by Corion (Patriarch) on Aug 14, 2012 at 17:28 UTC

    If you used strict, Perl would tell you where you are going wrong.

    Hint: $item and %item are different variables in Perl.

    Also, I wouldn't use lock when using queues. A queue should do all the locking your infrastructure needs. Consider communicating back to your master thread using queues. Also consider not using a DBI handle in more than one thread. Personally, I would only create the DBI handle within one thread.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://987415]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found