As others have said, the simple solution is to use fork() to create a bunch of sub processes to handle each file. The problem with that approach is that if you have thousands of files, then the simple solution would create thousands of sub processes, which will bring you computer to a crawl.

If your requirements are simple, and you are running under unix/linux, then you could use the -P argument to xargs.

eg: Your processing script takes one input file, and can calculate the output file for itself.

cd ~/directory/with/files/to/process ls -1 | xargs -n1 -P 10 perl -w process_script.pl -args

Corion Suggested the use of Dominus's RunN Script which does broadly the same thing as xargs.

If I where in your situation, and my requirements where to complex for xargs, then I would write a script around Parallel::ForkManager.

This makes it possible to have more complex logic to decide what the output file should be from each input file, or to process some files in different ways. You also get a nice callback mechanism to handle errors and the like, all while the number of concurrent processing threads is limited to a number you specify.

Of course you could achieve the same by writing your own code using fork and signals, but why bother when there is already something available and debugged on CPAN.


In reply to Re: Running parallel processes without communication by chrestomanci
in thread Running parallel processes without communication by vit

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.