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); }
In reply to HTTP Requests and Threading by carlriz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |