http://www.perlmonks.org?node_id=777204

adismaug has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I am trying to write a script that will open a TCP socket, perform an HTTP get and display the server replay.
The script should fork and all the children should use the same socket (using HTTP version 1.1 and keep alive). The script below works fine but each process open a new socket which adds unnecessary delay (I am using Time::HiRes to calculate the total time to get 10 pages and save the content).
How can I improve the script so all the forked process wil use the same TCP socket?
Your help please…

#!/usr/bin/perl use LWP::Simple; use Time::HiRes qw(time); use Parallel::ForkManager; my $filename = "/tmp/result.log"; while ($z <=200) { $z++; my $i = 1; my $pm = new Parallel::ForkManager(20); my $t0 = time; my $num = 10; unlink("$filename"); open FH, ">:utf8", $filename; while ($i < $num) { $i++; $pm->start and next; $contents = get("http://test.com/b.php?page=$i"); print FH "$contents\?"; $pm->finish; } $pm->wait_all_children; $elapsed = time - $t0; print "$elapsed elapsed\n"; }
Regards,
Adi.