2015-03-31 111 views
0
sub handle_sigterm { 
    my @running = threads->list(threads::running); 
    for my $thr (@running) { 
     $thr->kill('SIGTERM')->join(); 
    } 
    threads->exit; 
} ## end sub handle_sigterm 


OUTPUT: 
Perl exited with active threads: 
     1 running and unjoined 
     0 finished and unjoined 
     1 running and detached 

看起来像handle_sigterm退出时没有清理线程?Perl:退出前清理活动线程

我能做些什么清理线程?

回答

2

threads->exit不会做你认为的事情。它退出当前线程,不是所有线程。在线程之外,就像调用exit一样。

threads->exit() 
    If needed, a thread can be exited at any time by calling 
    "threads->exit()". This will cause the thread to return "undef" in 
    a scalar context, or the empty list in a list context. 

    When called from the main thread, this behaves the same as exit(0). 

你想要的是要么等待所有线程完成...

$_->join for threads->list; 

或者脱离所有的线程,他们将在程序退出时终止。

$_->detach for threads->list; 

此外,要使用threads->list获得所有非固定,非分离线程的列表,运行与否。 threads->list(threads::running)只会给你仍在运行的线程。如果任何线程已完成但尚未加入,则将被错过。