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

Hello, can someone explain to me why this script runs fast at first and then it becomes slower and slower. Basically I have this script and also myClass perl class created and what myClass does is do 3 simple get requests using LWP::UserAgent. It starts out fast, and then later it becomes slower and slower to the point where it's very slow, but if I start it again and modify some code to begin where i left off, it starts fast again. Is there anyway to have it work at the same speed? The workers don't take very long to do 3 simple get requests, so the workers aren't stalling at all.
#!/usr/bin/perl use threads; use Thread::Queue; use myClass; use warnings; use Fcntl ':flock'; my $q = Thread::Queue->new(); open(FILE,"database.txt"); open(FILE2,">>answers.txt"); $q->enqueue($_) while(<FILE>); 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; }

Replies are listed 'Best First'.
Re: Slow worker threads
by BrowserUk (Patriarch) on Jul 18, 2009 at 00:36 UTC

    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.
Re: Slow worker threads
by jrsimmon (Hermit) on Jul 17, 2009 at 23:54 UTC

    I don't see anything obviously wrong with your code. A couple of suggestions for tracking it down:

    • Add some debug statements to let you know what the children are doing -- When it becomes slow, are all of them still active?
    • Are you running out of some resource, like memory?
    • Using something like Benchmark to find where the slowness is being introduced.

    This looks to me like it needs some straightforward debugging to figure out where your bottleneck is.

Re: Slow worker threads
by ig (Vicar) on Jul 18, 2009 at 06:09 UTC

    This is not related to your performance problem, but I wonder if flock is doing what you think it is. Consider the following:

    use strict; use warnings; use threads; use Fcntl qw(:flock); open(FILE, ">>/tmp/test.txt"); for(0..9) { threads->new(\&worker, $_); } $_->join for threads->list; sub worker { my $x = shift; flock(FILE, LOCK_EX); print "$x: locked file\n"; sleep(10); flock(FILE, LOCK_UN); print "$x: unlocked file\n"; return; }

    Which produces

    0: locked file 1: locked file 2: locked file 3: locked file 4: locked file 5: locked file 6: locked file 7: locked file 8: locked file 9: locked file 0: unlocked file 1: unlocked file 2: unlocked file 3: unlocked file 4: unlocked file 5: unlocked file 6: unlocked file 7: unlocked file 8: unlocked file 9: unlocked file

    If you are locking against other processes changing the file between your writes, you should probably be seeking to end of file between locking and writing.

      Let's consider NONBLOCKING flock , there's something really wrong going on :

      use strict; use warnings; use threads; use feature 'say' ; use Fcntl qw(:flock); open(FILE, ">>/tmp/test.txt"); for(0..9) { threads->new(\&worker, $_); } $_->join for threads->list; sub worker { my $x = shift; my $locked; $locked = flock(FILE, LOCK_EX|LOCK_NB ); if( $locked ) { say "$x: lock acquired"; sleep 4; } else { say "$x: cannot acquire lock"; } flock( FILE, LOCK_UN ); print "$x: lock released\n"; return; }
      0: lock acquired 1: lock acquired 2: lock acquired 3: lock acquired 4: lock acquired 5: lock acquired 6: lock acquired 7: lock acquired 8: lock acquired 9: lock acquired 0: lock released 1: lock released 2: lock released 3: lock released 4: lock released 5: lock released 6: lock released 7: lock released 8: lock released 9: lock released

      How come the lock is acquired again and again without being released anywhere in between ?!

      Isn't this what a lock is supposed to do ? Lock something and not give access to anyone else until released ?

        it didn't occur to me that the threads were actually sharing the FILE filehandle , so if one acquired the lock , each of them actually acquired the lock because FILE was shared amongst them( thanks tye for pointing that out )

        I managed to pull of a quick replacement for that which works as one would expect.

        Notice the 9-$x , that is to make sure that time(thread1)>time(thread2)>time(thread3)>... .

        Also notice that $thread_lock are thread-specific locks

        use strict; use warnings; use threads; use feature 'say' ; use Fcntl qw(:flock); my $file_path = ">>/tmp/test.txt"; open(FILE, $file_path); for(0..9) { threads->new(\&worker, $_); } $_->join for threads->list; sub worker { my $x = shift; my $locked; open(my $thread_lock,$file_path); $locked = flock($thread_lock, LOCK_EX|LOCK_NB ); if( $locked ) { say "$x: lock acquired"; sleep 9-$x; } else { say "$x: cannot acquire lock"; }; if($locked) { flock( $thread_lock, LOCK_UN ); print "$x: lock released\n"; } return; }
        0: lock acquired 1: cannot acquire lock 2: cannot acquire lock 3: cannot acquire lock 4: cannot acquire lock 5: cannot acquire lock 6: cannot acquire lock 7: cannot acquire lock 8: cannot acquire lock 9: cannot acquire lock 0: lock released
Re: Slow worker threads
by pome23 (Initiate) on Jul 18, 2009 at 14:34 UTC
    Hi, I think doing it this way has fixed my problem. This VPS that i'm working with is kind of low in resources. Anyways, is this a bad thing to do?
    #!/usr/bin/perl # use threads; use Thread::Queue; use warnings; use Fcntl ':flock'; sub process { my $i = shift; my $q = Thread::Queue->new(); $q->enqueue($_) for($i .. $i+9000); $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) { print $item."\n"; } return; } my $i = 0; until($i > 50000) { &process($i); $i+=9000; }