Perl子例程超时

Perl子例程超时,第1张

概述我有一个子程序,通常需要1秒才能运行.有时,它可以无限运行.如果子程序花费太长时间(> 10秒)并且忽略该子程序的运行,我想继续执行代码.这是我到目前为止使用的警报. use Win32::OLE;eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 10; # schedule al 我有一个子程序,通常需要1秒才能运行.有时,它可以无限运行.如果子程序花费太长时间(> 10秒)并且忽略该子程序的运行,我想继续执行代码.这是我到目前为止使用的警报.

use Win32::olE;eval {     local $SIG{ALRM} = sub { dIE "alarm\n" };    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永远不会返回,所以我无法继续前进.我也无法将这段代码放在该子程序中.有什么想法吗?

解决方法@H_502_17@ 我怀疑你想要做的事情本身就不可能在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)

总结

以上是内存溢出为你收集整理的Perl子例程超时全部内容,希望文章能够帮你解决Perl子例程超时所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1271340.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存