zoukankan      html  css  js  c++  java
  • AnyEvent::HTTP 实现异步请求

    异步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 实现异步请求
    

  • 相关阅读:
    不写代码能实现APP消息推送吗
    如何让智能设备接入天猫精灵,实现语音控制功能
    给GoKit3(STM32)装一块N102,在家就能体验NB-IoT开发啦
    ESP8266 NodeMcu机智云SOC方案开发经验分享
    利用map和reduce编写一个str2float函数
    代码学习(1)
    箱线图boxplot()的绘制
    mysql远程访问数据库的问题解决
    codeforces 596 C. p-binary
    主席树的妙用——Just h-index
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6200167.html
Copyright © 2011-2022 走看看