threads and logfile rotation
- From: Thomas Kratz <ThomasKratz@xxxxxxxxxxxxxxxx>
- Date: Mon, 30 Jul 2007 18:56:40 +0200
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)
TIA
Thomas
use strict;
use warnings;
use threads;
use IO::File;
my $fn = 'log/test.log';
my $fh = IO::File->new($fn, '>') or die "open failed, $!";
$fh->autoflush(1);
threads->new(sub { while(1) { sleep(1) } } )->detach();
my $i = 1;
my $j = 1;
while (1) {
if ( $i % 10 == 0 ) {
$fh->close() or die "close failed, $!";
rename($fn, "$fn." . $j++) or die "rename failed, $!";
$fh = IO::File->new($fn, '>') or die "open failed, $!";
$fh->autoflush(1);
}
print $fh "sequence: ", $i++, "\n";
sleep(1);
}
--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
.
- Follow-Ups:
- Re: threads and logfile rotation
- From: zentara
- Re: threads and logfile rotation
- Prev by Date: Re: fork command.
- Next by Date: Re: Perl CGI Oralce database error
- Previous by thread: Perl CGI Oralce database error
- Next by thread: Re: threads and logfile rotation
- Index(es):