Hi JediWombat,
The following provides a parallel demonstration to process all 3 LDIF files simultaneously. It requires MCE 1.830 minimally due to MCE::Relay failing from setting the input separator. That said, am posting the code before releasing 1.830 in order to have the perlmonks URL for the changelog.
Well, here is the code.
#!/usr/bin/perl
use strict;
use warnings;
# Requires MCE 1.830. Fixes MCE::Relay stalling from setting $/.
use MCE::Loop 1.830;
my $mbnum = $ARGV[0] or die "usage: $0 mbnum\n";
my @ldif_files = qw(
/path/to/file1.ldif.bz2
/path/to/file2.ldif.bz2
/path/to/file3.ldif.bz2
);
MCE::Loop->init(
max_workers => scalar @ldif_files,
chunk_size => 1,
init_relay => ''
);
mce_loop {
# my ($mce, $chunk_ref, $chunk_id) = @_;
# When chunk_size 1 is specified, $_ contains
# the same value as $chunk_ref->[0].
my ($file, $ret) = ($_, '');
# Must localize $/ to not stall MCE, fixed in 1.830.
# Localizing $/ is recommended, but fixed MCE if not.
local $/ = "";
open my $fh, "-|", "/usr/bin/bzcat $file"
or warn "open error ($file): $!";
if (defined fileno($fh)) {
while (<$fh>) {
if (/uid=$mbnum/m) {
$ret = "## $file\n";
$ret .= $_;
last;
}
}
close $fh;
}
# Relay is beneficial for running a code block serially
# and orderly. The init_relay option enables MCE::Relay.
# All participating workers must call relay. Here, workers
# write to STDOUT orderly starting with chunk_id 1.
MCE::relay { print $ret };
} \@ldif_files;
MCE::Loop->finish;
MCE 1.830 will be released on CPAN no latter than Monday, the 7th of August. In the meantime, the MCE Github repository is current.
Regards, Mario
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.