If the hit of processing the 20,000 files in one chunk is giving you greif, don't do that:)

Rather than scanning all the files and building an array of the results in one go, and then processing them in what ever way. you have to, why not overlap the finding of the files with the processing?

You could do this by getting one file at a time from readdir and the processing them in a while loop

opendir my $dh, '/path/to/files', or die ...; while( my file = readdir $dh ) { next unless $file =~ m[\.xml$]; # do something with the file } closedir $dh;

However, you could (maybe) get a more efficient overlap if you read the directory in one thread and process the matched files in another. Depending on the nature of the load on the machine in question, the filesystem and various other inponderables, this should allow the processing thread to process stuff whilst the read thread is blocked in IO states waiting for the OS.

This might be a starting point if the idea interests you. Try varying the -N option to adjust the buffer size so that the processing thread always has a file to precess when it is ready, without the size of the Q becoming unwieldy.

#! perl -slw use strict; require 5.008; use threads qw[yield]; use threads::shared; use Thread::Queue; use vars qw[$N]; die "Usage: $0 [-N=nn] dir .*\.xml" unless @ARGV == 2; $N ||= 100; my $signal : shared = 0; my $Q = Thread::Queue->new(); sub readdir_asynch { my ($dir, $mask) = @_; print $mask; opendir my $dh, $dir or die "Couldn't open $dir"; while( not $signal ) { yield if $Q->pending > $N; my $file = readdir( $dh ); last unless defined $file; $Q->enqueue( $dir . '/' . $file ) if $file =~ m[^$mask$]; } $Q->enqueue( "QUITING!!" ); } my $thread = threads->create( \&readdir_asynch, $ARGV[0], $ARGV[1] ); yield; while( ( my $file = $Q->dequeue ) ne 'QUITING!!' ) { printf "%s [%d]\n", $file, -S $file; } $thread->join;

Setting $signal to a non-zero value will cause the read thread to terminate before it completes reading the files, and allow a clean exit from the main thread, if that becomes necessary.

Caveat: You might need to change the 'QUITING!!' message if your ever likely to have a file with that name.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller



In reply to Re: Filesystem: How to grab subset of directory structure by BrowserUk
in thread Filesystem: How to grab subset of directory structure by P0w3rK!d

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.