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!
In reply to Re: Slow worker threads
by BrowserUk
in thread Slow worker threads
by pome23
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |