Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

It is possible to share a filehandle from multiple threads (but there may be caveats). The problem is how to pass the handle (an object in Perl's terms) to the thread that is going to perform the further processing as iTthreads won't allow you to share objects.

The solution is to pass the fileno associated with the filehandle and then "dup" the handle within the thread using the syntax  open my $newhandle, "<&=$fileno" ... Note:That is "<&=$fileno" (an alias file descriptor) not "<&$fileno" (a duplicate file descriptor) (see perlopentut for details). Note: Although this uses the open built-in syntax, it is not reopening the file, mearly duplicating the internal control structures required for accessing the existing open filehandle.

This incomplete demo, open files in the main thread, prints the first 10 lines before passing the fileno for that filehandle to a newly created thread that prints the rest of the file, closes it and dies:

#! perl -slw use strict; use threads; use threads::shared; sub thread{ my( $fileno ) = @_; open my $fh, "<&=$fileno" or warn $! and die; printf "%d:%s", threads->self->tid, $_ while defined( $_ = <$fh> ) +; close $fh; } for my $file ( map{ glob $_ } @ARGV ) { open my $fh, '<', $file or warn "$file : $!" and next; printf "From main: %s", scalar <$fh> for 1 .. 10; printf "Fileno:%d\n", fileno $fh; threads->create( \&thread, fileno( $fh ) )->detach; printf 'paused:';<STDIN>; }

This seems to work for simple filehandles and sockets, but I see no reason (but have not verified) why it should not also work for those created using Filehandle, just use the fileno method.

The one caveat I am aware of is that I have occasionally seen strange effects when trying this with files opened for read-write access, but it is transient and usually "goes away". That is to say, I've only seen it occasionally and it has always appeared to fix itself when other bugs where resolved, but I have done very limited testing with this. If you wish to pursue this, you will be a pioneer. Feedback welcomed.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

In reply to Re: FileHandles and threads by BrowserUk
in thread FileHandles and threads by sodul

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found