mojoshaneman has asked for the wisdom of the Perl Monks concerning the following question:
--------------------------------------- perl1.pl --------------------------------------- use strict; use warnings; use v5.8; use threads; use threads::shared; use HTTP::Cookies; use LWP::UserAgent; use HTTP::Request::Common qw(POST); use HTTP::Request::Common qw(POST $DYNAMIC_FILE_UPLOAD); use URI::Escape; use perl_module1; ##################################################### # main() ##################################################### print("Starting LetsRock() \n"); LetsRock(); print("LetsRock() ended \n"); exit(0); ###################################################################### +############################# # LetsRock ###################################################################### +############################# sub LetsRock { my (@kids); for (my $x=0; $x < 5; $x++) { my ($kid) = Thread(\&ThreadFunc, 'https://www.wellsfargo.com') +; if (defined($kid)) { push(@kids, $kid); } sleep(1); } WaitForThreads(@kids); } ###################################################################### +############################# # ThreadFunc ###################################################################### +############################# sub ThreadFunc { my ($url) = @_; GetIt($url); } ###################################################################### +####### # Function: int Thread(ref function, string[] params) # # Description: # Spawn a thread to execute the specified function with optiona +l parameters # # Scope: public # # Parameters: # ref function - reference to function to call in thread # string[] params - optional params to pass to function # # Possible return values: id of spawned thread or undef ###################################################################### +####### sub Thread { my ($function, @params) = @_; my ($thread); if (defined(&{$function})) { $thread = threads->create($function, @params); } return($thread); } ###################################################################### +####### # Function: void WaitForThreads(int[] threads) # # Description: # Wait for all threads to end and return # # Scope: public # # Parameters: # int[] threads - array of thread ids # # Possible return values: none ###################################################################### +####### sub WaitForThreads { my (@threadArray) = @_; print("WaitForThreads() started \n"); foreach my $thread (@threadArray) { print("waiting for " . $thread->tid . "\n"); $thread->join(); } print("WaitForThreads() finished \n"); } ###################################################################### +####### # Function: void WaitForThread(int thread) # # Description: # Wait for a thread to end and return # # Scope: public # # Parameters: # int thread - thread id # # Possible return values: none ###################################################################### +####### sub WaitForThread { my ($thread) = @_; $thread->join; } --------------------------------------- perl_module1.pm --------------------------------------- package perl_module1; use strict; use v5.8; use HTTP::Cookies; use LWP::UserAgent; #use HTTP::Request::Common qw(POST); use HTTP::Request::Common; use URI::Escape; use vars qw(@ISA @EXPORT); use Exporter (); our @ISA = qw(Exporter); our @EXPORT = qw( GetIt ); our $DEBUG = 0; ###################################################################### +# # GetIt ###################################################################### +# sub GetIt { my ($url) = @_; my ($ua) = LWP::UserAgent->new(keep_alive => 1); $ua->timeout(60); # allow redirects for POST push @{ $ua->requests_redirectable }, 'POST'; # set the cookie my ($cookie) = './ac_cookie.txt' . $$; $ua->cookie_jar(HTTP::Cookies->new('file' => $cookie)); my ($response) = $ua->get($url); if ($response->is_success) { my ($page) = $response->content; print(); } } 1;
Edit: g0n - readmore tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Threads calling LWP causes exception
by BrowserUk (Patriarch) on Mar 10, 2006 at 01:22 UTC | |
by mojoshaneman (Novice) on Mar 10, 2006 at 02:31 UTC | |
by BrowserUk (Patriarch) on Mar 10, 2006 at 04:27 UTC | |
by mojoshaneman (Novice) on Mar 10, 2006 at 16:17 UTC | |
by BrowserUk (Patriarch) on Mar 10, 2006 at 18:01 UTC | |
| |
by BrowserUk (Patriarch) on Mar 10, 2006 at 03:10 UTC | |
by mojoshaneman (Novice) on Mar 10, 2006 at 05:24 UTC | |
by BrowserUk (Patriarch) on Mar 10, 2006 at 05:52 UTC |