Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^4: Using MCE to write to multiple files.

by BrowserUk (Patriarch)
on Nov 22, 2014 at 13:23 UTC ( [id://1108095]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Using MCE to write to multiple files.
in thread Using MCE to write to multiple files.

Damn! That's a complicated way of doing something very simple. It took me forever to work out where $chunkIds originated.

They start life as an integer list wrapped in a anonymous array:

threads->create( \&JobAdder, $q, $maxJobs, [ 0 .. 99 ] );

And get past individually to AddJob() here:

AddJob( $q, shift @$inputs );

Where they get pushed individually, but wrapped in individual anonymous arrays, onto the queue here:

$q->enqueue( [@_] );

Then those anonymous array refs get dequeued here:

while( defined( my $argRef = $q->dequeue ) ) {

And the individual integer are then unwrapped from them here:

$callBack->( @$argRef );

Before being passed to where they actually get used to number some files here:

sub GetFibby { my( $chunkId ) = @_; use autodie qw/ open close /; open my( $outfh ), '>', "fibojob-$chunkId.txt";

All of that to achieve the equivalent of:

$q->enqueue( 0 .. 99 );

Here's a simpler version that avoids the obfuscation:

#! perl -slw use strict; use threads; use threads::Q; sub fibonacci { my $n = shift; return undef if $n < 0; my $f; if( $n == 0 ) { $f = 0; } elsif( $n == 1 ) { $f = 1; } else { $f = fibonacci( $n - 1 ) + fibonacci( $n - 2 ); } return $f; } sub worker { my $tid = threads->tid; my( $Q, $path ) = @_; chdir $path or die "$path : $!"; while( defined( my $chunkId = $Q->dq ) ) { open my $out, '>', "fibojob-$chunkId.txt" or die "$chunkId : $ +!"; print $out join "\t", map fibonacci( $_ ), 1 .. 10; close $out; } } our $T //= 8; our $P //= '.'; our $M //= 99; ## The pattern is simple my $Q = threads::Q->new( $T*2 ); ## Cr +eate a queue. my @threads = map threads->create( \&worker, $Q, $P ), 1 .. $T; ## Cr +eate some workers to read from that queue. $Q->nq( 0 .. $M ); ## Qu +eue some work. $Q->nq( ( undef ) x $T ); ## Te +ll the workers they are done. $_->join for @threads; ## An +d wait for them to finish.

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".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2024-04-23 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found