Perl:每 15 秒调用一个子程序
Perl: Call a Subroutine Every 15 Seconds
所以我一直在这里以及 perl.org 和 perlmaven.com 上四处看看,但我对我正在尝试 运行。我用谷歌搜索了 $SIG 是什么,并在 here 上找到了一些东西,让我对 local $SIG 是什么有了一些了解,但我仍然对他的内容感到困惑意思是:
Perl has a built-in hash called %SIG, in which the keys are the names of the signals available in you operating system. The values are subroutines (more specifically references to subroutines), that will be called when the specific signal arrives.
In addition to the standard signals of your operating system Perl added two internal "signals". One of them is called WARN and is triggered every time some code calls the warn() function. The other one is called DIE and it is triggered when die() is called.
它还在 here 上说我不应该在闹钟呼叫中混用睡眠呼叫,但那里的解释对我来说似乎是另一种语言。 我很困惑。我在退出子程序时遇到了麻烦,但它并没有死在我身上。 我正在 运行ning 的代码是这个(我从这里的另一个 post 获得它,但我不想像 2011 年那样恢复 post):
sub checkfind {
didfindchange;
while ($call_counter <= 20) {
my $previous_call_finished = 0;
eval {
local $SIG{ALRM} = sub { die "Alarm.\n" };
alarm 15;
if ($did_find_change eq "false") {
didfindchange;
print("Original FIND value: $original_find_value\n");
print("FIND value: $ems_find_value\n");
}
elsif ($did_find_change eq "true") {
print("FIND changed.\n");
print("Original FIND value: $original_find_value\n");
print("FIND value: $ems_find_value\n");
$previous_call_finished = 1;
}
sleep;
};
#Propagate unexpected errors.
die unless $@ eq "Alarm.\n";
warn "Timed out!\n" unless $previous_call_finished
}
return;
}
我的程序一直死机,我不确定原因。任何帮助,将不胜感激。谢谢!
通常,人们会对可能需要很长时间的子例程/外部进程使用警报(等待数据库连接、网络资源、真正计算量大的计算等)。每次任务完成后,您都必须重置警报 alarm(0);
根据我的理解,您正在尝试做的事情是,像下面这样的简单睡眠/超时循环应该适合您。对于更复杂的事情(等待文件事件、网络事件),您可能希望考虑使用 EV 事件循环进行编程 perldoc EV or the AnyEvent library perldoc AnyEvent
#!/usr/bin/env perl
use warnings;
use strict;
use constant TIMEOUT => 10;
use constant SLEEP => 2;
check_for_something();
exit 0; # Success
sub check_for_something {
my $start_time = time();
my $elapsed = 0;
while ( $elapsed < TIMEOUT ) {
print "$elapsed seconds have elapsed...";
my $value = look_for_something();
print "Value is " . ( $value ? 'true' : 'false' ) . "\n";
if ($value) {
print "Value is true...returning\n";
return "value has changed...";
}
sleep SLEEP;
$elapsed = time() - $start_time;
}
die "Timed out after " . TIMEOUT . " seconds!\n";
}
sub look_for_something {
# !! changes the number into either '1' or 'undef'
# ! !! inverts the sense
return ! !! int rand (2 * TIMEOUT / SLEEP); # False most of the time, occasionaly true
}
输出
perl test_find.pl
0 seconds have elapsed...Value is false
2 seconds have elapsed...Value is false
4 seconds have elapsed...Value is false
6 seconds have elapsed...Value is false
8 seconds have elapsed...Value is false
Timed out after 10 seconds!
perl test_find.pl
0 seconds have elapsed...Value is false
2 seconds have elapsed...Value is false
4 seconds have elapsed...Value is true
Value is true...returning
所以我一直在这里以及 perl.org 和 perlmaven.com 上四处看看,但我对我正在尝试 运行。我用谷歌搜索了 $SIG 是什么,并在 here 上找到了一些东西,让我对 local $SIG 是什么有了一些了解,但我仍然对他的内容感到困惑意思是:
Perl has a built-in hash called %SIG, in which the keys are the names of the signals available in you operating system. The values are subroutines (more specifically references to subroutines), that will be called when the specific signal arrives.
In addition to the standard signals of your operating system Perl added two internal "signals". One of them is called WARN and is triggered every time some code calls the warn() function. The other one is called DIE and it is triggered when die() is called.
它还在 here 上说我不应该在闹钟呼叫中混用睡眠呼叫,但那里的解释对我来说似乎是另一种语言。 我很困惑。我在退出子程序时遇到了麻烦,但它并没有死在我身上。 我正在 运行ning 的代码是这个(我从这里的另一个 post 获得它,但我不想像 2011 年那样恢复 post):
sub checkfind {
didfindchange;
while ($call_counter <= 20) {
my $previous_call_finished = 0;
eval {
local $SIG{ALRM} = sub { die "Alarm.\n" };
alarm 15;
if ($did_find_change eq "false") {
didfindchange;
print("Original FIND value: $original_find_value\n");
print("FIND value: $ems_find_value\n");
}
elsif ($did_find_change eq "true") {
print("FIND changed.\n");
print("Original FIND value: $original_find_value\n");
print("FIND value: $ems_find_value\n");
$previous_call_finished = 1;
}
sleep;
};
#Propagate unexpected errors.
die unless $@ eq "Alarm.\n";
warn "Timed out!\n" unless $previous_call_finished
}
return;
}
我的程序一直死机,我不确定原因。任何帮助,将不胜感激。谢谢!
通常,人们会对可能需要很长时间的子例程/外部进程使用警报(等待数据库连接、网络资源、真正计算量大的计算等)。每次任务完成后,您都必须重置警报 alarm(0);
根据我的理解,您正在尝试做的事情是,像下面这样的简单睡眠/超时循环应该适合您。对于更复杂的事情(等待文件事件、网络事件),您可能希望考虑使用 EV 事件循环进行编程 perldoc EV or the AnyEvent library perldoc AnyEvent
#!/usr/bin/env perl
use warnings;
use strict;
use constant TIMEOUT => 10;
use constant SLEEP => 2;
check_for_something();
exit 0; # Success
sub check_for_something {
my $start_time = time();
my $elapsed = 0;
while ( $elapsed < TIMEOUT ) {
print "$elapsed seconds have elapsed...";
my $value = look_for_something();
print "Value is " . ( $value ? 'true' : 'false' ) . "\n";
if ($value) {
print "Value is true...returning\n";
return "value has changed...";
}
sleep SLEEP;
$elapsed = time() - $start_time;
}
die "Timed out after " . TIMEOUT . " seconds!\n";
}
sub look_for_something {
# !! changes the number into either '1' or 'undef'
# ! !! inverts the sense
return ! !! int rand (2 * TIMEOUT / SLEEP); # False most of the time, occasionaly true
}
输出
perl test_find.pl
0 seconds have elapsed...Value is false
2 seconds have elapsed...Value is false
4 seconds have elapsed...Value is false
6 seconds have elapsed...Value is false
8 seconds have elapsed...Value is false
Timed out after 10 seconds!
perl test_find.pl
0 seconds have elapsed...Value is false
2 seconds have elapsed...Value is false
4 seconds have elapsed...Value is true
Value is true...returning