异步http:
jrhmpt01:/root/async# cat a1.pl
use LWP::UserAgent;
use utf8;
use DBI;
use POSIX;
use HTTP::Date qw(time2iso str2time time2iso time2isoz);
use Data::Dumper;
use HTML::TreeBuilder;
use HTML::TreeBuilder::XPath;
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
$ua->agent("Mozilla/8.0");
#my $response = $ua->get('http://data.10jqka.com.cn/financial/yjyg/date/2016-03-31/board/ALL/field/enddate/order/desc/page/1/ajax/1/');
#my $response = $ua->get('http://data.10jqka.com.cn/financial/yjyg/');
$time1=time2iso(time());
print "$time1 is $time1
";
my $response = $ua->get('http://120.55.118.6:3000/api/env?ip=192.168.32.101');
if ($response->is_success) {
print $response->decoded_content; # or whatever
}else{print $response->decoded_content; };
$time2=time2iso(time());
print "$time2 is $time2
";
print "111111111111111111111111111111111111
";
jrhmpt01:/root/async#
jrhmpt01:/root/async#
jrhmpt01:/root/async# perl a1.pl
$time1 is 2016-04-16 18:25:47
500 read timeout
$time2 is 2016-04-16 18:25:57
111111111111111111111111111111111111
堵塞直到超时,此时不能干任何事情,需要异步请求来处理!
jrhmpt01:/root/async# cat a3.pl
#!/usr/bin/perl
use AnyEvent;
use AnyEvent::HTTP;
my $cv = AnyEvent->condvar;
sub doit{
my $url = shift ;
return if not defined $url;
$cv->begin;
http_get( "$url", sub { done( $url, @_ ) } );
print "1111111111111111
";
}
sub done {
my ($url, $content, $hdr) = @_;
$cv->end();
print "Search: $url Status: ", $hdr->{Status}, "
";
print "$content is $content
";
};
&doit('http://120.55.118.6:3000/api/env?ip=192.168.32.101');
print "222222222222222222
";
$cv->recv();
jrhmpt01:/root/async# perl a3.pl
1111111111111111
222222222222222222
Search: http://120.55.118.6:3000/api/env?ip=192.168.32.101 Status: 200
$content is ["","192.168.32.101 dr-mysql env-backup"]
利用AnyEvent::HTTP 实现异步请求