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;
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.