carlriz has asked for the wisdom of the Perl Monks concerning the following question:
Hey guys! I am completely new to this site, and I started learning perl a couple of weeks ago. I find it fascinating! So, the reason why I am here is to get some input on the script I wrote... Criticize and critique...How can I make this script better?
#!/usr/bin/perl + + use 5.012; use warnings; use threads; use threads::shared; use Thread::Queue; use LWP::UserAgent; use Time::HiRes qw( time ); use constant THREADS => 50; use Data::Dumper; $| = 1; ######### + + # Main # + + ######### + + my $queue = Thread::Queue->new(); my @URLs = qw( http://www.google.com/)x50; my @threads; my $time = time; my $json = '{"username": "anonymous", "password":"anonymous"}'; my %query_hash = (action => 'submit or status', name => 'chris', outn +ame => 'chris', 'content-type' => 'application/json'); print"\n\ Test...\n\n"; my $counter_thread : shared; for (1..THREADS) { push @threads, threads->create(sub { my $ua = LWP::UserAgent->new; $ua->timeout(5); # short timeout for easy testing. + + while(my $task = $queue->dequeue) { my $request = HTTP::Request->new(POST=>$task); $request->header(%query_hash); $request->content($json); my $response = $ua->request($request);; print"\n\n", $counter_thread++,"th :", "Dumped Data Return +ed:\n\n ", Dumper($response->decoded_content), "\n" ; print "Response code: ", $response->code, "\tHTTP Respons +e Message: ", $response->message, " ", "\n"; } }); } $queue->enqueue(@URLs); $queue->enqueue(undef) for 1..THREADS; Pending_HTTP(); Print_File(); {$_-> join foreach @threads}; ############################################## + + # Print pending HTTP 'POST' requests to file # + + ############################################## + + sub Pending_HTTP { my $file_write = 'Out_Stats.txt'; open(FILE, '>', $file_write) or die $!; while (!($queue->pending() == 0)) { sleep(1); my $time_now = time; print FILE "Number of pending HTTP 'POST' Requests: ", $queue- +>pending(), "\tTime Elapsed: ", ($time_now - $time), "\n"; } close(FILE); } ################################# + + # Print file to standard output # + + ################################# sub Print_File { print "\n\nListing Pending HTTP Requests and Time elapsed...\n\n"; open(FILE, '<', 'Out_Stats.txt') or die $!; print "**********************************************\n\n"; my $i = 1; while(defined($_ = <FILE>)) { print "$i: ", $_; $i++; } close(FILE); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HTTP Requests and Threading
by kcott (Archbishop) on Mar 06, 2014 at 19:25 UTC | |
by carlriz (Beadle) on Mar 06, 2014 at 19:43 UTC | |
by BrowserUk (Patriarch) on Mar 06, 2014 at 20:44 UTC | |
by carlriz (Beadle) on Mar 07, 2014 at 16:10 UTC | |
by PerlSufi (Friar) on Mar 06, 2014 at 19:49 UTC | |
|
Re: HTTP Requests and Threading
by kcott (Archbishop) on Mar 06, 2014 at 19:53 UTC | |
by carlriz (Beadle) on Mar 07, 2014 at 16:11 UTC | |
|
Re: HTTP Requests and Threading
by zentara (Cardinal) on Mar 07, 2014 at 11:34 UTC | |
|
Re: HTTP Requests and Threading
by crusty_collins (Friar) on Mar 06, 2014 at 19:04 UTC | |
by kcott (Archbishop) on Mar 06, 2014 at 19:31 UTC | |
by tye (Sage) on Mar 07, 2014 at 00:05 UTC | |
by kcott (Archbishop) on Mar 07, 2014 at 01:21 UTC | |
by runrig (Abbot) on Mar 07, 2014 at 16:49 UTC | |
by tye (Sage) on Mar 07, 2014 at 02:33 UTC | |
|
Re: HTTP Requests and Threading
by mje (Curate) on Mar 07, 2014 at 09:08 UTC |