in reply to Use more than one threads for one file processing

Hi heavenfeel,

The following is a demonstration using the MCE::Loop module. What is nice about MCE is the ability to process a file containing multiple records. The record separator for a fastq file is "\n@" which anchors @ at the start of the line. MCE detects "\n" at the start of the record separator.

MCE is a chunking engine allowing a worker to receive several records at a time. The effect is a reduction in the number of trips to and from the MCE-manager process.

The logic below allows one to search multiple patterns. Simply change the patterns to suite your needs. Perhaps, this can read patterns stored in a file. Anyway, this is a small MCE demonstration. The MCE->print statement prints the entire record to STDOUT.

use strict; use warnings; use MCE::Loop; my @patterns = ( "biopattern1", "biopattern2", "biopattern3" ); my $search = join('|', @patterns); my $regex = qr/$search/; open my $fh, "gunzip -c in.fastq.gz |" or die "open error: $!"; MCE::Loop->init( max_workers => 4, chunk_size => 50, RS => "\n@" ); MCE::Loop->run( sub { my ( $mce, $chunk_ref, $chunk_id ) = @_; for my $i ( 0 .. $#{ $chunk_ref } ) { if ( $chunk_ref->[$i] =~ /$regex/ ) { MCE->print( $chunk_ref->[$i] ); } } }, $fh ); MCE::Loop->finish(); close $fh;
Kind regards, Mario