T
Thomas Kratz
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^.-
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^.-