如何在 Perl 中的 x 秒后创建中断?

How to create interrupt after x seconds in Perl?

让我用代码示例来解释任务:

#!/usr/bin/perl
use strict;
use warnings;

my $t = 3;

eval {
    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
    print "Start $t\n";
    alarm(10);
    sleep($t);  # instead of this I have some data that collect code
    alarm(0);
    print "done with $t\n";
};

if ($@) {
    die unless $@ eq "alarm\n";
    print "timeout\n";
}

而不是 sleep 我有一些将数据推送到数组的代码。数组将保证在 `x 秒内由所需数据填充。

问题:如何在 x 秒后打印数组,而不使用 sleep(非阻塞方式)?

据我所知,在 perl 中设置计时器的最简单方法是使用 $SIG{ALRM}。但是,如果我不需要计时器(不能使用 sleep)怎么办,我只需要设置一个中断,该中断必须在预定义的秒数后 运行?也许我应该使用 SIGINT 来完成这项任务?

感谢任何帮助。

要创建您自己的中断,您需要两个执行线程。一种方法是启动一个子进程,该子进程将在满足某些条件时向其父进程发出信号。

$SIG{USR1} = \&code_to_run_after_interrupt;
my $ppid = $$;          # process id of parent
if (fork() == 0) {
    # child process
    sleep 15;
    kill 'USR1', $ppid;
    exit;
}
... main execution thread

fork 调用 15 秒后,您的主脚本将停止其正在执行的操作,执行名为 code_to_run_after_interrupt 的子例程中的代码,然后恢复主线程的执行。

(我在这里使用SIGUSR1因为处理SIGINT可能会让你无法使用Ctrl-C来停止你的程序)

这就是 alarm 的重点!只需使用更合适的信号处理程序即可。

my @x;

$SIG{ALRM} = sub {
   print("$_\n") for @x;
   $SIG{ALRM} = undef;
};

alarm(10);

...