Re: threads and logfile rotation



On Mon, 30 Jul 2007 18:56:40 +0200, Thomas Kratz
<ThomasKratz@xxxxxxxxxxxxxxxx> wrote:

I am a bit stumped with handling logfile rotation with a threaded app.
The below test script dies with "rename failed, Permission denied".
I assume this is because of the detached thread still referencing the
filehandle because it got copied at thread creation.

Moving the thread creation before creation of the file solves this, but
it is not really what I want. Ideally the thread should be able to log
as well to the copied handle.

Is there a way around this?

(perl 5.88 under Win23 with threads 1.64)


I don't know how this will work on win32, but the preffered way
of having threads share a filehandle, is by passing in the fileno;
usually through a shared variable, but this passes it in directly.

You can go both ways with this. You can create the filehandle in the
thread and place it's fileno in a shared variable, and the main thread
can then access it.

#!/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;
}
__END__


zentara
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
.



Relevant Pages

  • Re: threads and logfile rotation
    ... I assume this is because of the detached thread still referencing the filehandle because it got copied at thread creation. ... Signaling all the threads to close the filehandle and waiting for all of them to signal back 'done' is more than ugly. ... That way my existing logfile rotation can be used without changes. ...
    (comp.lang.perl.misc)
  • Re: threads and logfile rotation
    ... But the problem remains how to get the filehandle closed again to be ... you can probably work out an elegant loop where you write ... to the fileno, unless a shared flag is set. ...
    (comp.lang.perl.misc)
  • Re: perl problem with select and non-blocking sysread from multiple pipes
    ... > the indirect filehandle stuff is just plain confusing. ... >> What are you expecting fileno() to return for a filehandle that has ... D:\Home> perldoc -f fileno ... vec EXPR,OFFSET,BITS ...
    (comp.lang.perl.misc)
  • Re: Detecting filehandles
    ... do some check on $arg to see if it is a filehandle or a normal scalar ... Use fileno() for that, if it is defined then it is a valid filehandle. ... $ perl -le' ...
    (comp.lang.perl.misc)
  • Re: How to tell if handle is open?
    ... >How can my script tell whether a filehandle is open? ... Check for a nonzero return value from fileno. ...
    (comp.lang.perl.misc)