in reply to How to set pipe first and then use the forkmanager?

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.

Replies are listed 'Best First'.
Re^2: How to set pipe first and then use the forkmanager?
by mlin (Novice) on Sep 23, 2016 at 09:40 UTC
    I'm not familiar with MCE module, maybe I'll test it later.Thanks!