加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl子例程超时

发布时间:2020-12-15 23:37:33 所属栏目:大数据 来源:网络整理
导读:我有一个子程序,通常需要1秒才能运行.有时,它可以无限运行.如果子程序花费太长时间( 10秒)并且忽略该子程序的运行,我想继续执行代码.这是我到目前为止使用的警报. use Win32::OLE;eval { local $SIG{ALRM} = sub { die "alarmn" }; alarm 10; # schedule al
我有一个子程序,通常需要1秒才能运行.有时,它可以无限运行.如果子程序花费太长时间(> 10秒)并且忽略该子程序的运行,我想继续执行代码.这是我到目前为止使用的警报.

use Win32::OLE;

eval { 
    local $SIG{ALRM} = sub { die "alarmn" };
    alarm 10;                   # schedule alarm in 10 seconds 
    &do_the_subroutine;
    alarm 0;                    # cancel the alarm
};

if ($@) {
    $error_string .= $script;
    #Do something else if the subroutine took too long.
}

do_the_subroutine{
# use existing instance if Excel is already running
    eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')};
    die "Excel not installed" if $@;
    unless (defined $ex) {
        $ex = Win32::OLE->new('Excel.Application',sub {$_[0]->Quit;})
                or die "Oops,cannot start Excel";
    }

    # get a new workbook
    $book = $ex->Workbooks->Add;

    # write to a particular cell
    $sheet = $book->Worksheets(1);
    $sheet->Cells(1,1)->{Value} = "foo";

    # write a 2 rows by 3 columns range
    $sheet->Range("A8:C9")->{Value} = [[ undef,'Xyzzy','Plugh' ],[ 42,'Perl',3.1415  ]];

    # print "XyzzyPerl"
    $array = $sheet->Range("A8:C9")->{Value};
    for (@$array) {
        for (@$_) {
            print defined($_) ? "$_|" : "<undef>|";
        }
        print "n";
    }

    # save and exit
    $book->SaveAs( 'test.xls' );
    undef $book;
    undef $ex;
}

& do_the_subroutine永远不会返回,所以我无法继续前进.我也无法将这段代码放在该子程序中.有什么想法吗?

解决方法

我怀疑你想要做的事情本身就不可能在Windows上发出警报.

来自perldoc perlport:

alarm Emulated using timers that must be explicitly polled whenever Perl wants to dispatch “safe signals” and therefore cannot interrupt blocking system calls. (Win32)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读