Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks! I have a script where I'm writing a lot of files. It slows my computer up quite a bit and I've done some reading on it and it looks like making the file handle hot might be the answer - as far as freeing up memory and speeding things up. I can see that $| looks to be the key. Any help on modifying the code below would to make the file handle hot so I'm not killing my computer would be greatly appreciated
my $calls_dir3 = "Bing/1Parsed/Html2/"; opendir( my $search_dir2, $calls_dir3 ) or die "$!\n"; my @files = grep /\.txt$/i, readdir $search_dir2; closedir $search_dir2; print "Got ", scalar @files, " files\n"; # proxies open my $fh2, '<', 'proxies.txt' or die $!; chomp( my @proxies = <$fh2> ); close $fh2; foreach my $file (@files) { my %seen = (); my $current_file = $calls_dir3 . $file; my $proxy = shift @proxies; $proxy; print "Current proxy:$proxy\n"; open my $FILE, '<', $current_file or die "$file: $!\n"; my $x=1; make_path('Bing/1Parsed/Html3/'); while ( my $row = <$FILE> ) { open my $fh1, ">", "Bing/1Parsed/Html3/$file.$x.html" or die("Could not open file. $!"); #my $rnumber2 = rand(1999); $x = $x + 1; chomp $row; print "$row\n"; my $xml1 = $row; $fh1->print ("<meta name=" . chr(34) . "keywords" . chr(34 +) . "content=" . chr(34) . ($row) . chr(34) .">"); # create useragent my $ua = LWP::UserAgent->new; $ua->agent('Mozilla/5.0'); $ua->timeout('180'); # Use this UA/Proxy to fetch something.... $ua->proxy( ['http'], 'http://' . $proxy ); my $xml2 = get $xml1; $xml2; #add pause #my ( $x, $y ) = ( 1, 2 ); #my $result = int( rand( $y - $x + 1 ) ) + $x; #print "pausing "; #print $result; #print "seconds"; #sleep ($result); $fh1->print("\n"); print $xml2; $fh1->print($xml2); close $fh1; $xml2 = 1; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Make a file handle hot
by choroba (Cardinal) on Mar 08, 2016 at 14:52 UTC | |
|
Re: Make a file handle hot
by neilwatson (Priest) on Mar 08, 2016 at 15:01 UTC | |
|
Re: Make a file handle hot
by kennethk (Abbot) on Mar 08, 2016 at 19:56 UTC | |
|
Re: Make a file handle hot
by perlfan (Parson) on Mar 09, 2016 at 04:11 UTC |