Re: threads and logfile rotation
- From: zentara <zentara@xxxxxxxxxxxxxx>
- Date: Tue, 31 Jul 2007 12:38:03 GMT
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
.
- Follow-Ups:
- Re: threads and logfile rotation
- From: Thomas Kratz
- Re: threads and logfile rotation
- References:
- threads and logfile rotation
- From: Thomas Kratz
- threads and logfile rotation
- Prev by Date: Variable declaration - C vs script style
- Next by Date: Re: getting arguments
- Previous by thread: threads and logfile rotation
- Next by thread: Re: threads and logfile rotation
- Index(es):
Relevant Pages
|
|