tekio has asked for the wisdom of the Perl Monks concerning the following question:
I'm really having a difficult time finding a good example of how to do this. All the posts I find using Google, the poster has asked the question and just been told the they, and I are threading the application is incorrect.
But nobody has given an example of a proper way to do this. Also, most of the PERL threading tutorials I find are just printing out something from a thread, in a "hello world" type example.
As a random project, I have created an application that reads a list of words from a text file. It then uses LWP or IO::Socket to check and see if they are a valid domain on the Interwebz. (yes, I could use whois, but that is not the point).
After about 5 minutes, all my swap space is eaten up and the o/s comes to a screeching halt. I understand why. All my threads are taking up resources not being freed. I am doing something wrong.
What I want to do is read the test file; use a variable to define how many threads can be active at one time. Get back the memory from those threads, then do $n more threads at a time.
Below is my failed attempt: (i understand the code below probably sucks in all sorts of ways. Sorry if anyone is offended)
P.S. Merry Christmas!#!/usr/bin/perl use LWP::UserAgent; use threads; use threads::shared; our $totalCount :shared; $totalCount = 1; $count = 1; $k=1; $thrCount = 5; $timeout = 5; $start = localtime(); for($i=0; $i<=$#ARGV; $i++) { if($ARGV[$i] eq "\-t"){ $thrCount = $ARGV[$i+1]; } elsif($ARGV[$i] eq "\-i") { $infile = $ARGV[$i+ 1]; } } open(CONTENTS, "<$infile) or die("Could not open file!"); while($input = <CONTENTS>) { chomp $input; print "trying..... " . $input . "\n"; print " Total Count: " . $totalCount . " Count: " . $count . " +\n"; $thr = threads->new(\&tryHTTP,$input); $thr->detach(); $count++; $k++; if($k % $thrCount == 0) { sleep($timeout); $k=1; } } close(CONTENTS); $end = localtime(); print $start . "\n"; print $end . "\n"; print $totalCount . "/ " . $count . "\n"; sub tryHTTP { $ua = LWP::UserAgent->new; $ua->timeout((10)); $url = "http://www." . $_[0] . ".com"; $response = $ua->get($url); if($response->is_success) { $totalCount++; $response = ""; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Proper way to thread this in PERL.
by jethro (Monsignor) on Dec 25, 2013 at 20:33 UTC | |
by tekio (Novice) on Dec 25, 2013 at 21:33 UTC | |
|
Re: Proper way to thread this in PERL.
by Preceptor (Deacon) on Dec 25, 2013 at 23:34 UTC | |
by tekio (Novice) on Dec 26, 2013 at 01:46 UTC | |
|
Re: Proper way to thread this in PERL. (mech lwp asynchronous)
by Anonymous Monk on Dec 25, 2013 at 23:32 UTC | |
|
Re: Proper way to thread this in PERL.
by locked_user sundialsvc4 (Abbot) on Dec 26, 2013 at 03:06 UTC |