Take a look at forks.

I typically would do what you are doing like this:

#!/usr/bin/perl -w use strict; use warnings 'all'; use POSIX 'ceil'; use forks; use forks::shared; my @unprocessed = get_data(); my @processed : shared = ( ); my $total_records = scalar(@unprocessed); my $max_workers = 5; my $chunk_size = ceil($total_records / $max_workers); while( my @chunk = splice(@unprocessed, 0, $chunk_size) ) { threads->create(\&process_chunk, \@chunk); }# end while() # Keep an eye on our progress: threads->create(sub { my $finished = scalar(@processed); print STDERR "\r$finished/$total_records"; }); # Now wait for everyone to finish... $_->join foreach threads->list; # Process a chunk of data: sub process_chunk { my @chunk = @_; while( my $item = shift(@chunk) ) { # Do something with $_, then... # Supposing $item has an id... lock(@processed); push @processed, $item->id; }# end while() }# end process_chunk()

In reply to Re: Using threads to run multiple external processes at the same time by jdrago_999
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.