Perl使用bash中的线程捕获Ctrl-C

sda*_*aau 4 linux bash perl multithreading sigint

虽然我看到如何在bash中使用Perl陷阱Ctrl-C(sigint) ; 我迷失了为什么它会因为线程而失败; 我正在尝试以下脚本:

#!/usr/bin/env perl

use threads;
use threads::shared; # for shared variables

my $cnt :shared = 0;

sub counter() {
  while (1) {
    $cnt++;
    print "thread: $cnt \n";
    sleep 1;
  }
}

sub finisher{
  ### Thread exit! ...
  print "IIII";
  threads->exit();
  die;
};

# any of these will cause stop of reaction to Ctrl-C
$SIG{INT} = \&finisher;
$SIG{INT} = sub {print "EEE\n" ;} ;
$SIG{INT} = 'IGNORE';

# setting to DEFAULT, brings usual behavior back
#~ $SIG{INT} = 'DEFAULT';

my $mthr = threads->create(\&counter);
$mthr->join();
Run Code Online (Sandbox Code Playgroud)

...一旦SIGINT处理程序设置为默认值以外的任何值(Ctrl-C导致退出),它基本上会导致脚本停止对Ctrl-C做出更多反应:

$ ./test.pl 
thread: 1 
^Cthread: 2 
^C^Cthread: 3 
^C^C^C^Cthread: 4 
thread: 5 
thread: 6 
thread: 7 
thread: 8 
Terminated
Run Code Online (Sandbox Code Playgroud)

...我必须sudo killall perl终止脚本.

在这些链接中有一些线程和Ctrl-C:

...但我不能说它是否能最终回答在bash中"perl"下的"捕获"Ctrl-C是否绝对不可能?

提前感谢任何答案,
干杯!


好吧,我想我得到了(但我要离开前面的条目(下面)作为参考......)

诀窍证明,从主SIGINT处理程序,必须通过kill- 通过线程发信号- 然后线程也需要一个单独的SIGINT处理程序(来自OP中的第一个链接); 而不仅仅是join(),需要在@ikegami答案中使用代码:

#!/usr/bin/env perl

use threads;
use threads::shared; # for shared variables

my $cnt :shared = 0;
my $toexit :shared = 0;


sub counter() {
  $SIG{'INT'} = sub { print "Thread exit\n"; threads->exit(); };
  my $lexit = 0;
  while (not($lexit)) {
    { lock($toexit);
    $lexit = $toexit;
    }
    $cnt++;
    print "thread: $cnt \n";
    sleep 1;
  }
  print "out\n";
}

my $mthr;

sub finisher{
  { lock($toexit);
  $toexit = 1;
  }
  $mthr->kill('INT');
};

$SIG{INT} = \&finisher;

$mthr = threads->create(\&counter);


print "prejoin\n";
#~ $mthr->join();

while (threads->list()) {
   my @joinable = threads->list(threads::joinable);
   if (@joinable) {
      $_->join for @joinable;
   } else {
      sleep(0.050);
   }
}
print "postjoin\n";
Run Code Online (Sandbox Code Playgroud)

我可能在$toexit那里过度使用它,但至少现在这是结果:

$ ./test.pl
prejoin
thread: 1 
thread: 2 
thread: 3 
^CThread exit
postjoin
Run Code Online (Sandbox Code Playgroud)

非常感谢所有的解决方案:)
干杯!

 


多亏了@mob建议创立PERL_SIGNALSunsafe(注,Perl的5.14不允许$ ENV {"PERL_SIGNALS"}的"内部"的设置),我得到的地方-现在检测按Ctrl-C -但它无论是与终止段错误或错误:

#!/usr/bin/env perl

use threads;
use threads::shared; # for shared variables

my $cnt :shared = 0;
my $toexit :shared = 0;

sub counter() {
  my $lexit = 0;
  while (not($lexit)) {
    { lock($toexit);
    $lexit = $toexit;
    }
    $cnt++;
    print "thread: $cnt \n";
    sleep 1;
  }
  print "out\n";
  #~ threads->detach(); # Thread 1 terminated abnormally: Cannot detach a joined thread
  #~ exit;
}

my $mthr;

# [http://code.activestate.com/lists/perl5-porters/164923/ [perl #92246] Perl 5.14 does not allow "internal" setting of $ENV ...]
sub finisher{
  ### Thread exit! ...
  #~ print "IIII";
  # anything here results with: Perl exited with active threads:
  #~ threads->exit();
  #~ threads->join();
  #~ $mthr->exit();
  #~ $mthr->join();
  #~ $mthr->detach();
  #~ $mthr->kill();
  #~ threads->exit() if threads->can('exit');   # Thread friendly
  #~ die;
  { lock($toexit);
  $toexit = 1;
  }
  #~ threads->join(); #
};




# any of these will cause stop of reaction to Ctrl-C
$SIG{INT} = \&finisher;
#~ $SIG{INT} = sub {print "EEE\n" ; die; } ;
#~ $SIG{INT} = 'IGNORE';

# setting to DEFAULT, brings usual behavior back
#~ $SIG{INT} = 'DEFAULT';

$mthr = threads->create(\&counter);
print "prejoin\n";
$mthr->join();
print "postjoin\n";
Run Code Online (Sandbox Code Playgroud)

通过上面的评论,该代码作出反应:

$ PERL_SIGNALS="unsafe" ./testloop06.pl
prejoin
thread: 1 
thread: 2 
thread: 3 
^Cthread: 4 
out
Segmentation fault
Run Code Online (Sandbox Code Playgroud)

如果我添加以下使用Perl :: Signals :: Unsafe的结果,结果是相同的:

$mthr = threads->create(\&counter);

UNSAFE_SIGNALS {
  $mthr->join();
};
Run Code Online (Sandbox Code Playgroud)

几乎在那里,希望有人可以在... :)

ike*_*ami 5

信号处理程序仅在Perl操作码之间调用.您的代码被阻止$mthr->join();,因此它永远不会处理信号.

可能的方法:

use Time::HiRes qw( sleep );

# Interruptable << $_->join() for threads->list; >>
while (threads->list()) {
   my @joinable = threads->list(threads::joinable);
   if (@joinable) {
      $_->join for @joinable;
   } else {
      sleep(0.050);
   }
}
Run Code Online (Sandbox Code Playgroud)