allhellno has asked for the wisdom of the Perl Monks concerning the following question:
Howdy monks! So I have been trying to code a script to fetch info for game, and I found with out threads this is simply to slow to be effective. However, In an attempt to make things faster I added threading, but now when I run the script it quickly shoots up to 4gb memory then crashes. Now I am completely new to this so some one school me on what I'm doing wrong so this never happens again. note, the var $names is 16k lines long
use threads; use warnings; use LWP::Simple; $names=''; my $re69='((?:[a-z0-9_]+))'; open (lookup, '>>rstlookup.txt'); my @a = (); my @b = (); print "Starting main program\n"; my $nb_process = 16832; my $nb_compute = 20; my $i=0; my @running = (); my @Threads; while (scalar @Threads < $nb_compute) { @running = threads->list(threads::running); while ($names =~ m/$re69/isg) { if (scalar @running < $nb_process) { $name = $1; my $thread = threads->new(\&lookup); push (@Threads, $thread); my $tid = $thread->tid; } @running = threads->list(threads::running); foreach my $thr (@Threads) { if ($thr->is_running()) { my $tid = $thr->tid; } elsif ($thr->is_joinable()) { my $tid = $thr->tid; $thr->join; } } } @running = threads->list(threads::running); $i++; } while (scalar @running != 0) { foreach my $thr (@Threads) { $thr->join if ($thr->is_joinable()); } @running = threads->list(threads::running); } sub lookup { my $name2 = $name; my $lookup = get("http://rscript.org/lookup.php?type=track&time=62 +899200&user=".$name2."&skill=all"); print "Looking up $name2...\n"; $reg1='(ERROR)'; if ($lookup) { my $re1='(gain)'; my $re2='(:)'; my $re3='(Overall)'; my $re4='(:)'; my $re5='(\\d+)'; my $re6='(:)'; my $re7='(\\d+)'; my $re=$re1.$re2.$re3.$re4.$re5.$re6.$re7; if ($lookup =~ m/$re/isg) { print lookup "$name2 $7\n"; } } if ($lookup =~ m/$reg1/isg) { print lookup "$name2 doesn't exist \ +n" } else{ print lookup "$name2 0\n"; } } close (lookup);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Consumes memory then crashs
by BrowserUk (Patriarch) on Mar 24, 2012 at 12:51 UTC | |
by allhellno (Novice) on Mar 24, 2012 at 13:20 UTC | |
by BrowserUk (Patriarch) on Mar 24, 2012 at 14:54 UTC | |
by allhellno (Novice) on Mar 24, 2012 at 15:25 UTC | |
by BrowserUk (Patriarch) on Mar 24, 2012 at 15:40 UTC | |
| |
by Corion (Patriarch) on Mar 24, 2012 at 15:27 UTC |