threads - Re: reopening STDERR in parent process

Posted on Fri Aug 29 15:22:43 2008 by jdhedden in response to 8691 (See the whole thread of 6)
Re: reopening STDERR in parent process
Each thread gets its own perl interpreter which is a clone of its parent when the the thread was created. As such, all file handles (including STDERR) are duplicated when a thread is created. Because of this, closing and reopening STDERR in one thread does not affect STDERR in other threads.

Also, signals are not propagated to each thread. Under Cygwin, for example, only the main thread interpreter gets them. (It may be that under other OS's the currently active interpreter will act on a signal. You'll have to test this to be sure.)

Using threads v1.27 or later, you can send signals to threads. The code below may work for you:
$SIG{'USR1'} = $rotateLogs; $SIG{'HUP'} = sub { # Signal all (running, non-detached) threads to rotate logs if ($threads::threads) { $_->kill('USR1') foreach threads->list('running'); } # Rotate logs in main thread $rotateLogs->(); };
Note, however, that the list of running threads does not include detached threads. You'll need to track them yourself, if needed.
Direct Responses: 8698 | 8700 | Write a response