Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: PerlIO file handle dup

by marioroy (Priest)
on Sep 03, 2018 at 00:16 UTC ( #1221594=note: print w/replies, xml ) Need Help??


in reply to PerlIO file handle dup

Hi chris212,

At the time of my last reply, I didn't realize you had made an update. Disclaimer: Oh, btw, I'm not here to push MCE. Please use the module of your liking. Depending on the OS and/or number of workers, MCE::Mutex may run faster than Thread::Semaphore.

Thread::Semaphore

use strict; use warnings; use threads; use Thread::Semaphore; use MCE::Shared; use Time::HiRes 'time'; my $condvar = MCE::Shared->condvar; my $sem = Thread::Semaphore->new; # Start the shared server. Not necessary if Perl has IO::FDPass. MCE::Shared->start; sub test { $condvar->wait; for (1..10000) { threads->yield; $sem->down; $sem->up; } } threads->create('test') for 1..3; $condvar->broadcast(0.5); my $start = time; $_->join for threads->list; printf "duration: %0.03f secs\n", time - $start;

MCE::Mutex::Channel

use strict; use warnings; use threads; use MCE::Mutex; use MCE::Shared; use Time::HiRes 'time'; my $condvar = MCE::Shared->condvar; my $mutex = MCE::Mutex->new; # Start the shared server. Not necessary if Perl has IO::FDPass. MCE::Shared->start; sub test { $condvar->wait; for (1..10000) { threads->yield; $mutex->lock; $mutex->unlock; } } threads->create('test') for 1..3; $condvar->broadcast(0.5); my $start = time; $_->join for threads->list; printf "duration: %0.03f secs\n", time - $start;

MCE::Mutex::Flock

use strict; use warnings; use threads; use MCE::Mutex; use MCE::Shared; use Time::HiRes 'time'; my $condvar = MCE::Shared->condvar; my $mutex = MCE::Mutex->new( impl => 'Flock' ); # Start the shared server. Not necessary if Perl has IO::FDPass. MCE::Shared->start; sub test { $condvar->wait; for (1..10000) { threads->yield; $mutex->lock; $mutex->unlock; } } threads->create('test') for 1..3; $condvar->broadcast(0.5); my $start = time; $_->join for threads->list; printf "duration: %0.03f secs\n", time - $start;

Results

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

* 3 threads: CentOS Linux 7.3

Thread::Semaphore 0.386 secs MCE::Mutex::Channel 0.162 secs MCE::Mutex::Flock 0.144 secs

* 3 threads: Windows 7

Thread::Semaphore 0.293 secs MCE::Mutex::Channel 0.499 secs MCE::Mutex::Flock 0.498 secs

* 20 threads: CentOS Linux 7.3

Thread::Semaphore 41.897 secs MCE::Mutex::Channel 0.980 secs MCE::Mutex::Flock 0.702 secs

* 20 threads: Windows 7

Thread::Semaphore 35.521 secs MCE::Mutex::Channel 2.994 secs MCE::Mutex::Flock 3.322 secs

Regards, Mario

Replies are listed 'Best First'.
Re^2: PerlIO file handle dup
by marioroy (Priest) on Sep 03, 2018 at 01:17 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1221594]
help
Chatterbox?
and the web crawler heard nothing...

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



    Results (80 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!