简体   繁体   English

WWW :: Mechanize :: Timed https超时不起作用

[英]WWW::Mechanize::Timed https timeout does not work

So I've researched to the ends of the internet (at least I think so) about this issue. 所以我已经研究了关于这个问题的互联网的终点(至少我是这么认为的)。 I'm trying to set an alarm timeout of 60 seconds for a get() but it does not get caught and will run past 60 seconds, also any time the default timeout (180 sec) is reached for the www::mechanized::timed constructor, I get the error below: 我正在尝试为get()设置60秒的alarm超时但是它不会被捕获并且将超过60秒,也是在www :: mechanized ::达到默认超时(180秒)的任何​​时间定时构造函数,我得到以下错误:

Use of uninitialized value in addition (+) at /usr/lib/perl5/site_perl/5.10.0/WWW/Mechanize/Timed.pm line 52. 在/usr/lib/perl5/site_perl/5.10.0/WWW/Mechanize/Timed.pm第52行另外使用未初始化的值(+)。

code: 码:

use WWW::Mechanize::Timed;
use HTTP::Cookies;
use Try::Tiny;

my $ua = WWW::Mechanize::Timed->new(
autocheck => 0#turning off autocheck becuase any get errors will be fatal need to check ourselves
);

my $cookies = HTTP::Cookies->new(
autosave => 1
);

$ua->cookie_jar($cookies);

$ua->agent_alias("Windows IE 6");

try{
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 60;
$ua->get('https://secure.site.com'); #secure site that timed out
alarm 0;
} catch {
die $_ unless $_ eq "alarm\n";
print "page timed out after 60 seconds!\n";
exit;
};

my $total_time = sprintf '%.3f', ($ua->client_elapsed_time);

unless($ua->success){
print "Error: " . $ua->status;
exit;
}
...

I've gone over these questions to figure out how to get alarm to work without writing my own timeout function. 我已经解决了这些问题,想出如何在不编写自己的超时功能的情况下使警报工作。

Perl Mechanize timeout not working with https and Ways to do timeouts in Perl? Perl Mechanize超时无法使用https以及如何在Perl中执行超时?

So far I see recommendations for using LWPx::ParanoidAgent, not sure if I understand the "Use LWPx::ParanoidAgent and mix it into Mech" part 到目前为止,我看到了使用LWPx :: ParanoidAgent的建议,不确定我是否理解“使用LWPx :: ParanoidAgent并将其混合到Mech”部分

Possible to use timeout in WWW::Mechanize on https? 可以在https上的WWW :: Mechanize中使用超时吗?

or patching LWP::UserAgent with 或修补LWP :: UserAgent

http://search.cpan.org/~sharyanto/LWP-UserAgent-Patch-HTTPSHardTimeout-0.04/lib/LWP/UserAgent/Patch/HTTPSHardTimeout.pm http://search.cpan.org/~sharyanto/LWP-UserAgent-Patch-HTTPSHardTimeout-0.04/lib/LWP/UserAgent/Patch/HTTPSHardTimeout.pm

Any thoughts on how to get the timeout to work with alarm? 有关如何使超时与警报一起工作的任何想法?

Thanks! 谢谢!

The below helped to set an alarm for each get() , Seems much easier than try-catch with sig alarm unless i'm missing something? 以下有助于为每个get()设置一个警报,看起来比使用sig alarm try-catch容易得多,除非我遗漏了什么?

use Sys::SigAction qw(timeout_call);

if ( timeout_call( 60 ,sub { $ua->get('https://secured.site.com'); } ))
   {
print "ALARM page timed out after 60 seconds!\n" ;
exit;
}

Pretty much the same answer as this question but with actual code Ways to do timeouts in Perl? 与这个问题几乎相同的答案,但实际的代码如何在Perl中做超时?

text from http://metacpan.org/pod/Sys::SigAction 来自http://metacpan.org/pod/Sys::SigAction的文字

timeout_call() timeout_call()

$timeout ,$coderef

Given a code reference, and a timeout value (in seconds), timeout() will (in an eval) setup a signal handler for SIGALRM (which will die), set an alarm clock, and execute the code reference. 给定代码引用和超时值(以秒为单位),timeout()将(在eval中)设置SIGALRM的信号处理程序(将死亡),设置闹钟,并执行代码引用。 $time (seconds) may be expressed as a floating point number. $ time(秒)可以表示为浮点数。

If Time::HiRes is present and useable, timeout_call() can be used with a timer resolution of 0.000001 seconds. 如果Time :: HiRes存在且可用,则timeout_call()可以使用定时器分辨率0.000001秒。 If Time:HiRes is not available then factional second values less than 1.0 are tranparently converted to 1. 如果Time:HiRes不可用,那么小于1.0的派系第二个值会被转换为1。

If the alarm goes off the code will be interrupted. 如果警报响起,代码将被中断。 The alarm is canceled if the code returns before the alarm is fired. 如果代码在触发警报之前返回,则警报将被取消。 The routine returns true if the code being executed timed out. 如果正在执行的代码超时,则例程返回true。 (was interrupted). (被打断了)。 Exceptions thrown by the code executed are propagated out. 执行的代码抛出的异常会传播出去。

The original signal handler is restored, prior to returning to the caller. 在返回调用者之前,恢复原始信号处理程序。

If HiRes is not loadable, Sys::SigAction will do the right thing and convert 如果HiRes不可加载,Sys :: SigAction将做正确的事情并进行转换

one last thing to consider/keep in mind: 最后要考虑/记住的事情:

use of Sys::SigAction::timeout_call unsafe? 使用Sys :: SigAction :: timeout_call unsafe?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM