Seeing your having trouble with the forking, you could try this and see how it fairs. I've rather over-commented the code. HTH.

#! perl -slw use strict; use threads qw[ yield ]; use Thread::Queue; ## The command to run (DIR for testing. my $COMMAND = 'dir /s'; ## FIFOs in and out my $Qwork = new Thread::Queue; my $Qresults = new Thread::Queue; ## Track our workers my $workers :shared = 0; sub worker { $workers++; ## Count 'em in threads->self->detach; ## No return ## Wait until we got something to do yield until $Qwork->pending; ## Whilst there is work while ( $Qwork->pending ) { ## Grab some my $task = $Qwork->dequeue; ## do it and get the output open my $in, "$COMMAND $task 2>&1 |" ## Maybe queue it back rather than yelling or warn "command $task failed: $!"; ## Grab the output and post it back to the main thread ## Prefix with the work item if it must be segregated $Qresults->enqueue( $_ ) while <$in>; } $workers--; ## and count 'em out again } ## Start a pool of workers threads->new( \&worker ) for 1 .. 3; ## Give 'em something to do $Qwork->enqueue( $_ ) for qw[ D: P: Q: T: U: V: W: Z: ]; ## Drive to d +ir ## wait for something to do yield until $Qresults->pending; ## Workers running while( $workers ) { ## get a result if available my $result = $Qresults->dequeue if $Qresults->pending; ## Do something with $result printf $result if defined $result; ## Give up the timeslice if workers are running ## but we have no results yet yield while $workers and not $Qresults->pending; } ## All done.

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

In reply to Re: Parallel tasks by BrowserUk
in thread Parallel tasks by john.goor

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.