WWW :: Mechanize :: Timed https超时不起作用 [英] WWW::Mechanize::Timed https timeout does not work

查看:196
本文介绍了WWW :: Mechanize :: Timed https超时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我研究了这个问题的互联网(至少我是这么认为)。我正在尝试为 get()设置警报超时为60秒,但它不会被捕获并且将运行超过60秒,也是任何时候达到www :: mechanized :: timed构造函数的默认超时(180秒),我得到以下错误:

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:

在/usr/lib/perl5/site_perl/5.10.0/WWW/Mechanize/Timed.pm第52行另外使用未初始化的值(+)。

Use of uninitialized value in addition (+) at /usr/lib/perl5/site_perl/5.10.0/WWW/Mechanize/Timed.pm line 52.

代码:

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超时无法使用https

如何在Perl中执行超时?

到目前为止,我看到了使用LWPx :: ParanoidAgent的建议,而不是确定我是否理解使用LWPx :: ParanoidAgent并将其混合到Mech部分

So far I see recommendations for using LWPx::ParanoidAgent, not sure if I understand the "Use LWPx::ParanoidAgent and mix it into Mech" part

可以在WWW中使用超时::在https上使用机械化?

或修补LWP :: UserAgent with

or patching LWP::UserAgent with

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?

谢谢!

推荐答案

以下内容有助于为每个 get()设置一个警报,看起来比使用sig <$ c的try-catch更容易$ c>闹钟除非我遗漏了什么?

The below helped to set an alarm for each get(), Seems much easier than try-catch with sig alarm unless i'm missing something?

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;
}

与此问题几乎相同的答案,但实际代码如何在Perl中执行超时?

Pretty much the same answer as this question but with actual code Ways to do timeouts in Perl?

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


timeout_call()

timeout_call()

$ timeout,$ coderef

给定代码引用和超时值(
秒),timeout()将(在eval中)设置
SIGALRM(将死)的信号处理程序,设置闹钟,并执行代码
引用。 $ time(秒)可以表示为浮点
数。

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. $time (seconds) may be expressed as a floating point number.

如果Time :: HiRes存在且可用,则timeout_call()可以与
a计时器分辨率为0.000001秒。如果时间:HiRes不可用
,那么小于1.0的派系第二个值会被转换为
到1。

If Time::HiRes is present and useable, timeout_call() can be used with a timer resolution of 0.000001 seconds. If Time:HiRes is not available then factional second values less than 1.0 are tranparently converted to 1.

如果闹钟响了代码会被打断。如果代码在触发警报之前返回,则警报取消
。如果正在执行的代码超时,则例程
返回true。 (被打断了)。
执行代码抛出的异常传播出去。

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. (was interrupted). Exceptions thrown by the code executed are propagated out.

原始信号处理程序在返回
调用者之前被恢复。

The original signal handler is restored, prior to returning to the caller.

如果HiRes不可加载,Sys :: SigAction将做正确的事情并且
转换

If HiRes is not loadable, Sys::SigAction will do the right thing and convert

最后要考虑/记住的事情:

one last thing to consider/keep in mind:

使用Sys :: SigAction :: timeout_call unsafe?

这篇关于WWW :: Mechanize :: Timed https超时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆