in reply to Re^3: How to split file for threading?
in thread How to split file for threading?

Update: Added chunk_size option to the example.

Update: Added note on one worker reading at any given time.

The example by BrowserUk is awesome. Perhaps you're having to work with Perl not compiled with threads; e.g. running Perl on Solaris. A pattern counter can be done with MCE using the following code. The usage is the same between the two scripts.

This is safe from random I/O thrashing. The read pattern is sequential, not random between workers due to one worker reading at any given time.

#! perl -slw use strict; use warnings; use MCE::Loop; our $T //= 'auto'; my ( $file_name, $target ) = @ARGV; MCE::Loop::init { use_slurpio => 1, max_workers => $T, chunk_size => 1024 * 1024 * 16, }; my @result = mce_loop_f { my ( $mce, $slurped_chunk, $chunk_id ) = @_; my $count = 0; $count++ while ( $$slurped_chunk =~ /$target/g ); MCE->gather($count); } $file_name; my $total = 0; $total += shift @result while @result; print "Found $total '$target' lines";

Usage:

mce_script.pl -T=n theFile.txt "the string"