Multithreading Perl:信号和线程。如何使用qx()终止线程
我有一个脚本,可以解析日志并查找错误和警告。Multithreading Perl:信号和线程。如何使用qx()终止线程,multithreading,perl,signals,Multithreading,Perl,Signals,我有一个脚本,可以解析日志并查找错误和警告。 我想使用用户友好的方式解释此日志。 因此,我使用记事本 以下是代码: use v5.16; use strict; use warnings; use Win32::Clipboard; use threads; use utf8; my $kp = Win32::Clipboard->new(); my $output = shift || "out_log.txt"; #my ($input, $output)=@ARGV; #op
我想使用用户友好的方式解释此日志。
因此,我使用
记事本
以下是代码:
use v5.16;
use strict;
use warnings;
use Win32::Clipboard;
use threads;
use utf8;
my $kp = Win32::Clipboard->new();
my $output = shift || "out_log.txt";
#my ($input, $output)=@ARGV;
#open my $ih, "<", $input or die "can't open input file with log\n";
open my $oh, ">", $output or die "can't open output file with log\n";
my @alls=split /\n/,$kp->Get();
for my $k(0..$#alls){
$_ = $alls[$k];
if(/^ERR.+|^WARN.+/){
print {$oh} qq(at position $k --> ).$_."\n";
}
}
my $thread =
threads->create(sub{
$SIG{INT}=sub{die"All good\n";};
qx(notepad $output);
}
);
print qq(type 'y' for quit);
do{
print "want to quit?\n>" ;
chomp;
do{
say "I will kill this thread";
$thread->kill('INT') if defined($thread);
say "and delete output";
unlink $output;
exit(0);
}if (m/y/);
}while(<>);
使用v5.16;
严格使用;
使用警告;
使用Win32::剪贴板;
使用线程;
使用utf8;
我的$kp=Win32::剪贴板->新建();
my$output=shift | |“out_log.txt”;
#我的($input,$output)=@ARGV;
#打开我的$ih,“;
咀嚼;
做{
说“我要杀了这根线”;
$thread->kill('INT')如果已定义($thread);
说“并删除输出”;
取消美元输出的链接;
出口(0);
}若为(m/y/);
}while();
当我试图杀死运行记事本的线程时,它掉了下来。
如何做到这一点,使用信号和线程?可能吗?
以及您对解决方案的想法。
谢谢 这不起作用,因为您的SIGINT
从未传递到记事本。所以它永远不会关闭。(这个处理器可能永远不会被处理)
你需要以不同的方式处理这个问题。查看Win32::Process
,了解如何生成/终止记事本进程的一些示例
my $ProcessObj;
Win32::Process::Create( $ProcessObj,
"C:\\Windows\\system32\\notepad.exe",
"notepad", 0, NORMAL_PRIORITY_CLASS, "." )
or die $!;
然后你可以使用
$ProcessObj -> Kill(1);
我建议使用Thread::Semaphore
或某种共享变量来决定是否要杀死记事本