免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: iamlimeng
打印 上一主题 下一主题

用多线程和持续连接实现高速WEB请求 [复制链接]

论坛徽章:
0
1 [报告]
发表于 2010-06-17 11:25 |显示全部楼层
Coro直接带 Coro::Semaphore; 线程池 不用自己写
  1. use strict;
  2. use warnings;
  3. use Smart::Comments;

  4. use Coro;
  5. use Coro::LWP;
  6. use Coro::Semaphore;
  7. use LWP::UserAgent;
  8. use LWP::ConnCache;
  9. use constant LIMIT => 200;

  10. use Benchmark;
  11. my $TT0 = new Benchmark;
  12. my $request_times = 1000;
  13. print "\n Now begin testing ... \n";

  14. my $outpath = './data';
  15. my $url = 'http://www.baidu.com';

  16. my $semaphore = Coro::Semaphore->new(LIMIT);
  17. my (@coros,@error);

  18. my $ua    = new LWP::UserAgent;
  19. my $conncache = new LWP::ConnCache;
  20. $ua->conn_cache($conncache);

  21. for my $num (0..999) {
  22.         my $outfile = $outpath.'/'.$num.'.html';
  23.         my $outfile_tmp = $outfile.'.tmp';
  24.     push @coros, async {
  25.                my $guard = $semaphore->guard;
  26.        
  27.                 my $req = HTTP::Request->new(GET => $url);
  28.                 my $res = $ua->request($req);
  29.                                                                                                        
  30.                    if($res->is_success){
  31.                            my $html = $res -> content;
  32.                            open(FF,">",$outfile_tmp);
  33.                            print FF $html;
  34.                            close(FF);
  35.                           
  36.                            if(-e $outfile_tmp){
  37.                                    unlink $outfile;
  38.                                    rename $outfile_tmp,$outfile;
  39.                            }
  40.                            print $num,'ok',$/;
  41.                    }else{
  42.                            push @error,$url;
  43.                    }
  44.         };
  45. }
  46.        
  47. $_ -> join for @coros;
  48.        
  49. my $TT1 = new Benchmark;
  50. my $td = Benchmark::timediff($TT1, $TT0);
  51. $td = Benchmark::timestr($td);
  52. my ($sec) = ($td =~ /(\d+).*/);
  53. my $speed = sprintf("%0.1f",$request_times/$sec);
  54. print "\n Time expend: $td\n Average Speed: $speed Times Per Second\n\n Press Enter to close me ... \7";

复制代码

论坛徽章:
0
2 [报告]
发表于 2010-06-17 14:35 |显示全部楼层
回复 11# iamlimeng


   调整LIMIT的数值 现在是并行200个线程

论坛徽章:
0
3 [报告]
发表于 2010-06-20 18:34 |显示全部楼层
7L的不是线程池吧???线程池的目的就是为了避免频繁创造线程。7L的达不到这个目的啊,只是限制了同时执行 ...
starwing83 发表于 2010-06-18 22:38


coro这种协程的东西 不能用传统意义的知识标准去衡量 关键还是要看实际效果

就如同 你不能用传统的进程概念去形容erlang的进程

论坛徽章:
0
4 [报告]
发表于 2010-06-20 20:03 |显示全部楼层
但问题是,我用7l的代码发现CPU占用率比楼主的线程池要高(LIMIT已设为10)
starwing83 发表于 2010-06-20 18:49



    ググれカス
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP