in reply to File::Temp randomness when forking
The error occurs because you are calling tempdir before your loop. This makes File::Temp call srand internally, and all subsequent forks inherit the seeded random value.You can 'fix' this behaviour by explicitly calling srand($$) before your call to File::Temp->new() in the loop. You're probably right that it would be a good idea for the documentation to point out this particular quirk (or the module could work around it by comparing $$ between rand calls calling srand explicitly), I suggest you file a bug report at rt.cpan.org.
As an aside, you would probably have gotten a quicker response or even have found out the reason for yourself if the code you posted had been reduced to the bits necessary to demonstrate this bug. This would have sufficed (with the fix commented out):
use strict; use warnings; use Parallel::ForkManager; use File::Temp qw(tempdir tempfile); my $pm=new Parallel::ForkManager(15); my $tempdir = tempdir(); for my $i (1..15) { $pm->start and next; # srand($$); my $fh = File::Temp->new(TEMPLATE => "sappyXXXXXXXX", UNLINK => 0) or die "Could not make tem +pfile: $!"; close $fh or die "Could not close tempfile: $!"; $pm->finish(); } $pm->wait_all_children;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: File::Temp randomness when forking
by ryantate (Friar) on Nov 29, 2005 at 17:47 UTC | |
by tirwhan (Abbot) on Nov 29, 2005 at 17:57 UTC | |
by ryantate (Friar) on Nov 29, 2005 at 18:07 UTC |