Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Converting a parallel-serial shell script

by BrowserUk (Pope)
on Sep 18, 2008 at 12:50 UTC ( #712271=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Converting a parallel-serial shell script
in thread Converting a parallel-serial shell script

Updated: This modified version actually terminates. ++Corion

Needs some error checking, but this should work:

#! perl -slw use strict; use threads; use Thread::Queue; our $THREADS ||= 4; my $Qraw = new Thread::Queue; my $Qtsv = new Thread::Queue; sub convert{ $_[ 0 ] } sub toTSV { while( my $filename = $Qraw->dequeue ) { my $outFile = $filename . '.tsv'; open my $fhIn, '<', $filename or warn "$filename : $!" and nex +t; open my $fhOut, '>', $outFile or warn "$outFile : $!" and nex +t; while( <$fhIn> ) { my $tsv = convert( $_ ); print $fhOut $tsv; } close $fhOut; close $fhIn; $Qtsv->enqueue( $outFile ); } $Qtsv->enqueue( undef ); } my @threads = map threads->create( \&toTSV ), 1 .. $THREADS; ## Filenames from command line, 4 threads to terminate $Qraw->enqueue( @ARGV, (undef) x $THREADS ); for( 1 .. $THREADS ) { while( my $tsvFile = $Qtsv->dequeue ) { system "echo $tsvFile"; unlink $tsvFile; } } $_->join for @threads;

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.


Comment on Re^3: Converting a parallel-serial shell script
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2014-12-21 04:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (102 votes), past polls