I have a script that runs as a daemon. It scans a directory for files, processes the files, moves the files to backup, then repeats the process by scanning again for any new files that may have arrived while the others where processing. The processing of the files can take a few seconds to several minutes.
To make my process more efficient, I use afork to multithread a limited number of child processes.
sub afork (@$&) {
# First field = array, second field = max number of processes to
# run at the same time, third field = subroutine to run against
# each array element
my ($data, $max, $code) = @_;
my $c = 0;
foreach my $data (@$data) {
wait unless ++ $c <= $max;
die "Fork failed: $!\n" unless defined (my $pid = fork);
exit $code -> ($data) unless $pid;
}
1 until -1 == wait;
}
while(1) {
@FILES = `ls -1 $DIRECTORY`;
afork (\@FILES,3,\&process_file);
}
The problem is, groups of files tend to arrive in this directory all at once. If 10 files arrive and I send these files in an array to the afork routine that limits them to running 3 at a time, I must wait until all 10 files complete before I can scan the directory again for any new files. This is is troubling when I have one file in that group of 10 that takes 20 minutes to complete, but the rest of the files completed in a few seconds. I am forced to wait until the one large file completes before I can begin processing any new files.
Is there a way to update the array I pass to afork on the fly, or should I use something else altogether?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.