在UNIX风格的系统上,我会使用信号处理程序捕获ALRM事件,然后使用闹钟函数启动计时器,然后开始预计可能会超时的操作。如果操作完成,我会使用警报(0)关闭警报并正常退出,否则信号处理程序应该将其启动以正常关闭所有内容。
我有一段时间没有在Windows上使用Perl,而Windows有点POSIXy,我不能保证这会工作;您必须检查perl文档以查看您的平台上是否支持信号。
关于信号处理和使用alarm()这种自毁编程的更多详细信息可以在Perl Cookbook中找到。这里有一个简单的例子从另一个职位取消和修改了一点:
eval {
# Create signal handler and make it local so it falls out of scope
# outside the eval block
local $SIG{ALRM} = sub {
print "Print this if we time out, then die.\n";
die "alarm\n";
};
# Set the alarm, take your chance running the routine, and turn off
# the alarm if it completes.
alarm(90);
routine_that_might_take_a_while();
alarm(0);
};
你可以提供关于如何配置的更多信息吗? – 2008-09-16 18:17:02