Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
....funny no one mentioned that you can share a filehandle between threads thru the fileno, see FileHandles and threads

...here is an old node..... just put the logfile's fileno into a shared variable....then any thread can open it...... watch out for concurrency problems though

#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; # original idea from BrowserUK at # http://perlmonks.org?node_id=493754 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>; } sub thread{ my( $fileno ) = @_; open my $fh, "<&=$fileno" or warn $! and die; printf "%d:%s", threads->self->tid, $_ while defined( $_ = <$fh> ); close $fh; }

...this bit of code might interest you

======== zentara wrote: > I wouldn't give up too quickly on letting the threads close and > rename the file. > > First, all threads share the same filehandles thru the filenos. [snipped outline for shared writing to logfile] Heureka! My problem was not getting the logging sorted out. I didn't think that + you could close the filehandle from each thread. But one is actually required to, before the file is free to rename. I should have tried it + before. Thanks for your input! Thomas Here a small example that works as intended: use strict; use warnings; use threads; use Thread::Queue; use IO::File; my $q = Thread::Queue->new(); sub worker { my $fh; while (1) { my $fno = $q->dequeue(); if ( $fno ) { print "worker: opening fileno:$fno\n"; $fh = IO::File->new_from_fd($fno, '>') or die $!; $fh->autoflush(1); print $fh "worker was here\n"; } else { $fh->close(); } } } threads->new(\&worker)->detach(); my $fname = 'test.log'; my $fh; $fh = IO::File->new($fname, '>') or die $!; $fh->autoflush(1); print $fh "main was here\n"; $q->enqueue($fh->fileno()); sleep 1; $q->enqueue(0); sleep 1; $fh->close(); rename($fname, "$fname.1") or die $!; $fh = IO::File->new($fname, '>') or die $!; $fh->autoflush(1); print $fh "main was here\n"; $q->enqueue($fh->fileno()); sleep 1;

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

In reply to Re: [threads] Open a file in one thread and allow others to write to it by zentara
in thread [threads] Open a file in one thread and allow others to write to it by Gangabass

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 cooling their heels in the Monastery: (2)
As of 2024-04-26 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found