in reply to Re^6: how to split huge file reading into multiple threads
in thread how to split huge file reading into multiple threads
#!/usr/bin/perl use warnings; use strict; use threads; use IO::Select; use IO::Pipe; my @ranges = ( [1,10000000],[10000001,20000000],[20000001,30000000], [30000001,40000000],[40000001,50000000] ); my $sel = new IO::Select(); # thread launching foreach (@ranges){ my $pipe = IO::Pipe->new(); my $start = $_->[0]; my $end = $_->[1]; print "$start $end $pipe\n"; threads->create( \&thread, $start, $end, $pipe )->detach; # only call reader after pipe has been passed to thread $sel->add( $pipe->reader() ); } # watching thread output print "Watching\n\n"; while(1){ foreach my $h ($sel->can_read){ my $buf; if ( (sysread($h, $buf, 1024) > 0 ) ){ print "Main says: $buf\n"; } } } sub thread{ my( $start, $finish, $pipe ) = @_; my $wh = $pipe->writer; $wh->autoflush(1); print $wh "thread# ",threads->tid()," -> $start, $finish, $pipe \n" +; sleep 5; print $wh "thread# ",threads->tid()," -> finishing \n" ; sleep 2; } __END__
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: how to split huge file reading into multiple threads
by BrowserUk (Patriarch) on Sep 02, 2011 at 02:21 UTC | |
by zentara (Cardinal) on Sep 02, 2011 at 09:14 UTC | |
by BrowserUk (Patriarch) on Sep 02, 2011 at 10:15 UTC | |
by zentara (Cardinal) on Sep 02, 2011 at 15:24 UTC | |
by BrowserUk (Patriarch) on Sep 02, 2011 at 16:32 UTC | |
|