Greetings,

The following is a demonstration using MCE::Loop and MCE::Shared.

MCE::Shared v1.805 or later is required if running via Perl5i on a Unix platform.

use strict; use warnings; use MCE::Loop; use MCE::Shared; # Configure MCE to run with 4 workers. # A worker receives 1 item per iteration. MCE::Loop->init( max_workers => 4, chunk_size => 1, ); # Populate an array with test data. my @data_files = qw/ file1 file2 file3 file4 /; # Open a shared file handle to the external cat command. mce_open my $fh, "| cat" or die "open error: $!\n"; # Process the array in parallel. # Workers send data to the shared file handle. mce_loop { my $file = $_; printf $fh "wid: %d, name: %s\n", MCE->wid(), $file; } @data_files; # Close the shared file handle. close $fh;

The sample code generates the following output. It may differ depending on which worker obtains data first.

wid: 3, name: file1 wid: 2, name: file2 wid: 4, name: file3 wid: 1, name: file4

On Windows, replace the mce_open line with the following. This will work if Cygwin is installed on the C: drive. The code works with Strawberry Perl, ActiveState Perl, and Cygwin Perl.

mce_open my $fh, '| c:/cygwin/bin/cat.exe' or die "open error: $!\n";

Warm regards, Mario.


In reply to Re: How to set pipe first and then use the forkmanager? by marioroy
in thread How to set pipe first and then use the forkmanager? by mlin

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.