Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Using the OP's example, I modified it to do locking via MCE::Mutex. Afterwards, toyed with a couple MCE demonstrations. All demonstrations output orderly.

OP's example, locking via MCE::Mutex

#!/opt/perl/bin/perl use strict; use threads; use MCE::Mutex; use MCE::Shared; open my $fh, '|-', 'gzip > test.txt.gz'; foreach (1..10000) { print {$fh} sprintf("%05d%s\n", $_, ('abc123' x 10)); } close $fh; mce_open $fh,'-|','gzip -cd test.txt.gz' or die "Failed to uncompress: $!\n"; $| = 1; my @threads = (); my $mutex = MCE::Mutex->new(); foreach (1..3) { push @threads, threads->create(\&test); } $_->join() foreach @threads; close $fh; print "\n"; sub test { my $tid = threads->tid(); my $line; while(1) { threads->yield(); $mutex->lock(); $line = <$fh> or last; print "Thread $tid ".$line; $mutex->unlock(); } $mutex->unlock; }

MCE, no chunking

#!/opt/perl/bin/perl use strict; use threads; use MCE; open my $fh, '|-', 'gzip > test.txt.gz'; foreach (1..10000) { print {$fh} sprintf("%05d%s\n", $_, ('abc123' x 10)); } close $fh; open $fh, '-|', 'gzip -cd test.txt.gz' or die "Failed to uncompress: $!\n"; $| = 1; # MCE spawns threads when threads is present MCE->new( chunk_size => 1, max_workers => 3, input_data => $fh, init_relay => 1, user_func => sub { my ($mce, $chunk_ref, $chunk_id) = @_; my $tid = threads->tid(); MCE::relay sub { print "Thread $tid ".$chunk_ref->[0]; }; } )->run; close $fh; print "\n";

MCE, chunking enabled

#!/opt/perl/bin/perl use strict; use threads; use MCE; open my $fh, '|-', 'gzip > test.txt.gz'; foreach (1..10000) { print {$fh} sprintf("%05d%s\n", $_, ('abc123' x 10)); } close $fh; open $fh, '-|', 'gzip -cd test.txt.gz' or die "Failed to uncompress: $!\n"; $| = 1; # MCE spawns threads when threads is present MCE->new( chunk_size => 500, max_workers => 3, input_data => $fh, init_relay => 1, user_func => sub { my ($mce, $chunk_ref, $chunk_id) = @_; my $tid = threads->tid(); my $buf = ''; foreach my $line ( @{ $chunk_ref } ) { $buf .= "Thread $tid ".$line; } MCE::relay sub { print $buf; }; } )->run; close $fh; print "\n";

Results taken from a Linux CentOS 7.3 VM

My laptop is a late 2013 Macbook Pro, 2.6 Ghz i7 quad CPU. The Linux virtual machine is configured with 4 cores.

* 3 workers

time perl script_sem.pl | wc -l # 0.543s OP's example time perl script_mutex.pl | wc -l # 0.663s Ditto, MCE::Mutex time perl script_mce.pl | wc -l # 0.225s MCE, no chunking time perl script_chunk.pl | wc -l # 0.077s MCE, chunking enabled

* 10 workers

time perl script_sem.pl | wc -l # 1.072s OP's example time perl script_mutex.pl | wc -l # 0.726s Ditto, MCE::Mutex time perl script_mce.pl | wc -l # 0.255s MCE, no chunking time perl script_chunk.pl | wc -l # 0.112s MCE, chunking enabled

* 20 workers

time perl script_sem.pl | wc -l # 1.849s OP's example time perl script_mutex.pl | wc -l # 0.803s Ditto, MCE::Mutex time perl script_mce.pl | wc -l # 0.339s MCE, no chunking time perl script_chunk.pl | wc -l # 0.179s MCE, chunking enabled

Regards, Mario


In reply to Re^2: PerlIO file handle dup by marioroy
in thread PerlIO file handle dup by chris212

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (5)
    As of 2019-06-19 21:30 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Is there a future for codeless software?



      Results (89 votes). Check out past polls.

      Notices?