IF you could knock up a little code to show the flow -- it doesn't have to work, I can knock it into shape

Well I ran into some glitches, and the net result is it's probably easiest just to let each thread write to it's own separate file. Of course, your superior insights may see a way out.

I had to create a dummy file, in order to get some fileno's, and although select does seem to intercept the thread writes, they still go to file directly, so there dosn't seem to be any use for the select, except to intercept the data as it's being written to disk. Also the select seemed to repeat it's data reads, but that probably could be fixed.

Before I saw the above glitch, my idea was to have each thread search thru it's list for primes, and only print back to main when a prime was found in it's range.

Conclusion: My original suggestion of letting each thread print to it's own output file, and merging them after script completion, is probably best. Maybe if one used an event loop system, a filehandle watch could be used without the need for a disk file to get a fileno, but then you would be displaying results to a widget of some sort.

#!/usr/bin/perl use warnings; use strict; use threads; use IO::Select; use FileHandle; my @ranges = ( [1,10000000],[10000001,20000000],[20000001,30000000], [30000001,40000000],[40000001,50000000] ); my $sel = new IO::Select(); # thread launching foreach (@ranges){ my $fh = FileHandle->new(); open ($fh,'+>', './dummyfile'); # needed to get filehandle to give a fileno # maybe better to use IO::Handle and give it # a fileno directly? my $start = $_->[0]; my $end = $_->[1]; my $fileno = fileno($fh); print "$start $end $fileno\n"; threads->create( \&thread, $start, $end, $fileno )->detach; $sel->add($fh); } # watching thread output print "Watching\n\n"; #while( scalar (threads->list) > 0 ){ # dosn't seem to work while(1){ foreach my $h ($sel->can_read){ my $buf; if ( (sysread($h, $buf, 1024) > 0 ) ){ print "Main says: $buf\n"; #truncate $h, 0; # bad idea :-) } } } sub thread{ my( $start, $finish, $fileno ) = @_; open my $fh, ">&=$fileno" or warn $! and die; print $fh "thread# ",threads->tid()," -> $start, $finish, $fileno \n" + ; sleep 5; print $fh "thread# ",threads->tid()," -> finishing \n" ; } __END__

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

In reply to Re^7: how to split huge file reading into multiple threads by zentara
in thread how to split huge file reading into multiple threads by sagarika

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.