Define "slow down"? If you mean stall completely, then its (probably) because the threads have run out of data to process, but you haven't done anything to cause them to terminate.

You are queuing the entire contents of 'database.txt' before starting the threads, but once they have processed everything, the queue will be empty, so $q->dequeue() will block forever.

In the version below, I mocked up your myClass and added $q->enqueue( (undef) x 10 ); to cause the threads to terminate, and it process the 80,000 lines in my dictionary file in about 10 seconds, before exiting cleanly (Note:I've arranged for the output to go to the screen so I can see what is happening!):

#!/usr/bin/perl -l use warnings; use strict; use Fcntl ':flock'; use threads; use Thread::Queue; #use myClass; { package myClass; sub new{ bless {}, $_[0] } sub getRandom{ int rand 1000 } } my $q = Thread::Queue->new(); open(FILE,"database.txt") or die $!; open(FILE2,">>CON") or die $!; $q->enqueue($_) while(<FILE>); ### Cause the threads to terminate!!! $q->enqueue( (undef) x 10 ); for(0 .. 9) { threads->new(\&worker, $q); } $_->join for threads->list; sub worker { my $queue = shift; while( my $item = $queue->dequeue) { chomp($item); my $p = myClass->new(); my $physics = $p->getRandom(); flock(FILE2,LOCK_EX); print FILE2 "$item|$physics"; print "$item|$physics"; flock(FILE2,LOCK_UN); } return; }

If that fix doesn't do it for you, then it suggests the problem lies inside myClass, in which case it would be far easier to help you if you posted it!


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.
RIP PCW

In reply to Re: Slow worker threads by BrowserUk
in thread Slow worker threads by pome23

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.