Actually a simple boss/worker model should be easy enough. This wont scale to multiple computers but its a starting point. Note that this code was torn from a larger fully functional threaded program I have. The code below is not complete and not even tested but it should demonstrate all the concepts you need.

For example, how can I avoid a race condition when I'm filling up the result data structure (for the table) if the results themselves come in asynchronously?

You can put your work back into a queue to avoid a race condition. Alternatively look into lock(). Just make a variable called $lock then you can control access to your output structure. by doing something like this..

my $lock : shared; { lock($lock) ...do something that only 1 thread at a time should do... }

How do I tell which thread is busy and which one is available?

You shouldn't need to. In the scheme below using a queue an idle thread will pick up a unit of work if it can, if there is no more work it will exit and if no work exists yet it will wait.

use threads; use threads::shared; use Thread::Queue; my $max_threads = 8; my $queue = Thread::Queue->new(); print "Spawning threads..."; for(1...$max_threads){ threads->create(\&thread_main)->detach(); } print "Done!\n"; ...Do what you need to do to get a work unit... $queue->enqueue("SOME_WORK_UNIT"); #Signal to the threads they're done. for(1...$max_threads){ $queue->enqueue("DONE"); } wait_on_threads(); exit 0; sub thread_main(){ while(1){ my ($work) = $queue->dequeue()); # If end-of-queue marker reached then cleanup and exit. if($app eq "DONE"){ return 0; } ...do processing work... } } sub wait_on_threads(){ #Dont even start printing messages until our queue is near deplete +d. while($queue->pending() > $max_threads){ sleep 1; } my $cnt = 0; my $wait = 5; while(my $items = $queue->pending() > 0){ # Fortunatly on AIX sleep doesnt seem to put ALL the threads t +o sleep. # whew! This is a non-portable loop though ;) if ($cnt++ > $wait){ print "Please wait. $items items still in progress.\n"; $cnt = 0; $wait++; } sleep 3; } }

In reply to Re: Using threads to run multiple external processes at the same time by bot403
in thread Using threads to run multiple external processes at the same time by Anonymous Monk

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.