在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