in reply to Re^5: shared scalar freed early
in thread shared scalar freed early

That is exactly what I needed. The final piece of the puzzle to help me understand how to effectively use MCE in my script. It is now faster than ever, doesn't use any shared variables, and the parallelization is completely handled by MCE. It just took me a bit to realize I needed to remove my signal handlers. The relays must use SIGINT or something. It even takes my input file handle and does the chunked reading for me, and works with IPC file handles from the gzip command. The relay blocks as needed to keep output in sequence. Thanks a lot!

Replies are listed 'Best First'.
Re^7: shared scalar freed early
by marioroy (Prior) on Mar 08, 2017 at 21:02 UTC

    Below, an example using MCE::Flow. It chunks and outputs swiftly. The relay option when defined loads MCE::Relay and with that enables relay capabilities. Relay is beneficial in places where workers must run orderly and serially. Only a single worker can run inside the relay block. The important thing is that workers enter it orderly by "chunk id" value.

    If threads is desired on a Unix platform, simply load threads prior to loading MCE. By default, MCE spawns threads if present. Unlike MCE.pm where chunk_size defaults to 1, chunk_size is configured automatically for MCE models.

    use strict; use warnings; use MCE::Flow; ## Make gzip file { open my $fh, '|-', 'gzip > test.txt.gz'; foreach (1..100000) { print {$fh} sprintf('%04d',$_).('abc123' x 10)."\n"; } close $fh; } ## Read gzip file open my $fh, '-|', 'gzip -cd test.txt.gz' or die "open error: $!\n"; STDOUT->autoflush(1); # important MCE::Flow->init( max_workers => 3, init_relay => 0 ); sub test { my ($mce, $chunkref, $chunkid) = @_; my ($buf, $wid) = ('', MCE->wid()); for my $i (0 .. $#{ $chunkref }) { $buf .= $chunkref->[$i]; } MCE::relay { print "## worker: $wid, chunkid: $chunkid\n".$buf; }; } MCE::Flow->run_file(\&test, $fh); MCE::Flow->finish(); close($fh);

    Cheers, Mario.

Re^7: shared scalar freed early
by marioroy (Prior) on Mar 08, 2017 at 18:41 UTC

    You're welcome. Regarding relay capabilities, the worker who happens to have chunk 1 goes first, then worker with chunk 2 goes next, and so forth. In other words, order is by "chunk id" for entering the relay block.

    Regards, Mario.