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

Re: PerlIO file handle dup

by BrowserUk (Pope)
on Mar 07, 2017 at 07:31 UTC ( #1183815=note: print w/replies, xml ) Need Help??


in reply to PerlIO file handle dup

How does this compare with what you are doing?

#! perl -slw use strict; use threads; use threads::shared; our $T //= 8; my @buffers; share( $buffers[ $_ ] ) for 0 .. $T-1; my $stdoutSem :shared; sub reader { my $fname = shift; open my $fh, '<', $fname or die $!; my $next = 0; while( <$fh> ) { chomp; lock $buffers[ $next ]; $buffers[ $next ] = $_; $next = ++$next % $T; } close $fh; for( 0 .. $T -1 ) { lock $buffers[ $_ ]; $buffers[ $_ ] = undef; } } sub worker { my $tid = threads->tid; my $bufn = shift; my $localbuf; while( 1 ) { { lock $buffers[ $bufn ]; last unless defined( $buffers[ $bufn ] ); $localbuf = $buffers[ $bufn ]; } ## process localbuf here. { lock $stdoutSem; print "[$tid] processed record: '", $localbuf, "'"; } } } my $reader = threads->new( \&reader, $ARGV[ 0 ] ); my @workers = map threads->new( \&worker, $_ ), 0 .. $T-1; $reader->join; $_->join for @workers;

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: PerlIO file handle dup
by chris212 (Scribe) on Mar 07, 2017 at 14:55 UTC
    I'm not sure I tried passing data between threads with a shared array, but I think performance would be similar to a queue, which was too slow.
      but I think performance would be similar to a queue,

      The problem with a queue is that all locking is applied to the entire shared array, thus every lock blocks all contenders, even if they are after different elements of the array.

      If you look carefully at my example, you'll see that the @buffers array itself isn't (explicitly) shared and is never locked; only the per-thread scalar elements are.

      And as only the reader thread and 1 worker thread per buffer are competing for any given lock, all workers threads are free to continue independantly of each other.

      And finally, the lock on any given buffer is only held for the brief time it takes to copy its contents to a local buffer, thus the reader thread can be repopulating it with the next record whilst the worker thread is processing the previous one.

      The upshot is that in my use of this technique, it beats Thread::Queue by a wide margin for applications where the processing of a record takes 3x or more time, than that required to read it.

      Test it. You might be pleasantly surprised.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: PerlIO file handle dup
by chris212 (Scribe) on Mar 07, 2017 at 22:57 UTC
    You lose sequence. I had to autoflush output to keep it from getting jumbled. I didn't realize you can share elements of an array without sharing the whole array. Can you do the same with hashes?
      You lose sequence.

      Sorry, I hadn't realised that was a requirement. I'll think about that.

      Can you do the same with hashes?

      Yes. But be aware that if you don't lock the hash and insertions and deletions are going on, things can get a little strange. (Eg. Iterating the hash keys might return a key, but by the time you try to access the associate value, the key might have been deleted.

      If the hash is essentially static -- only the values changing -- then only sharing the value scalars and applying locking to them individually can be much more efficient than locking the entire hash for every update.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Actually it looks like if the hash itself is not shared, any changes to keys will not affect other running threads. This includes deleting keys (the key/value will still be there in other threads). Only changes to the shared values will affect other running threads (including undefining them).

        use strict; use threads; use threads::shared; my %hash = ( 'a' => 'foo', 'b' => 'foo', 'c' => 'foo', 'd' => 'foo' ); share($hash{$_}) foreach('a'..'f'); my $tid = 0; foreach my $key('a'..'f') { if(exists $hash{$key}) { if(defined $hash{$key}) { print "$key => $hash{$key} in thread $tid\n"; } else { print "$key => undef in thread $tid\n"; } } else { print "$key does not exist in thread $tid\n"; } } my $th1 = async { $tid = 1; print "thread $tid started\n"; sleep 2; foreach my $key('a'..'f') { if(exists $hash{$key}) { if(defined $hash{$key}) { print "$key => $hash{$key} in thread $tid\n"; } else { print "$key => undef in thread $tid\n"; } } else { print "$key does not exist in thread $tid\n"; } } print "delete b from thread $tid\n"; delete $hash{'b'}; print "undefine d from thread $tid\n"; undef $hash{'d'}; print "setting f => foo in thread $tid\n"; $hash{'f'} = 'foo'; }; my $th2 = async { $tid = 2; print "thread $tid started\n"; sleep 3; foreach my $key('a'..'f') { if(exists $hash{$key}) { if(defined $hash{$key}) { print "$key => $hash{$key} in thread $tid\n"; } else { print "$key => undef in thread $tid\n"; } } else { print "$key does not exist in thread $tid\n"; } } }; sleep 1; print "delete a from thread $tid\n"; delete $hash{'a'}; print "undefine c from thread $tid\n"; undef $hash{'c'}; print "setting e => foo in thread $tid\n"; $hash{'e'} = 'foo'; $th1->join(); $th2->join(); foreach my $key('a'..'f') { if(exists $hash{$key}) { if(defined $hash{$key}) { print "$key => $hash{$key} in thread $tid\n"; } else { print "$key => undef in thread $tid\n"; } } else { print "$key does not exist in thread $tid\n"; } }
        a => foo in thread 0 b => foo in thread 0 c => foo in thread 0 d => foo in thread 0 e => undef in thread 0 f => undef in thread 0 thread 1 started thread 2 started delete a from thread 0 undefine c from thread 0 setting e => foo in thread 0 a => foo in thread 1 b => foo in thread 1 c => undef in thread 1 d => foo in thread 1 e => foo in thread 1 f => undef in thread 1 delete b from thread 1 undefine d from thread 1 setting f => foo in thread 1 a => foo in thread 2 b => foo in thread 2 c => undef in thread 2 d => undef in thread 2 e => foo in thread 2 f => foo in thread 2 a does not exist in thread 0 b => foo in thread 0 c => undef in thread 0 d => undef in thread 0 e => foo in thread 0 f => foo in thread 0

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2019-05-26 06:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (153 votes). Check out past polls.

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