|
Wednesday, 23 September 2009 21:41 |
#!/usr/bin/perl use threads; use LWP;
my $PAGE = "http://www.example.com/";
# Ten concurrent threads my $numThreads = 10; my @threadHandles = (); my @results = ();
for ($i = 0; $i < $numThreads; $i++ ) { # create a thread, give it its number as an argument my $thread = threads->create( doFetch, $i, $PAGE ); push( @threadHandles, $thread ); }
# Run through all outstanding threads and record their results. while( $#threadHandles > 0 ) { my $handle = pop(@threadHandles); my $result = $handle->join(); push( @results, $result ); print "result: $result\n"; }
sub doFetch { my $threadNum = shift; my $URL = shift; my $browser = LWP::UserAgent->new; my $response = $browser->get( $URL ); return "thread $i " . $response->status_line; }
|