简体   繁体   English

如何让 LWP::UserAgent 缓存连接?

[英]How can I get LWP::UserAgent to cache connections?

In a perl script I'm using LWP::UserAgent like this:在 perl 脚本中,我正在使用 LWP::UserAgent,如下所示:

my $ua = LWP::UserAgent->new( keep_alive => 10 );

After doing $ua-get a few thousand times to fetch URLs from, say, https://example.com , I would expect $ua->conn_cache to have ten open connections.在执行 $ua-get 几千次以从https://example.com获取 URL 之后,我希望 $ua->conn_cache 有十个打开的连接。 But, if I use Data::Dumper and do:但是,如果我使用 Data::Dumper 并执行以下操作:

print Dumper( $ua->conn_cache );

I see only one connection:我只看到一个连接:

$VAR1 = bless( {
                 'cc_limit_total' => 10,
                 'cc_conns' => [
                                 [
                                   bless( \*Symbol::GEN1, 'LWP::Protocol::https::Socket' ),
                                   'https',
                                   'example.com:443',
                                   1638549871
                                 ]
                               ]
               }, 'LWP::ConnCache' );

Am I doing something wrong, or am I just misinterpreting what the dump of conn_cache is showing me?我做错了什么,还是我只是误解了 conn_cache 的转储向我展示的内容? For what it's worth, the URLs I'm fetching differ only in their query parameters.值得一提的是,我获取的 URL 仅在查询参数上有所不同。

The most efficient way to handle many sequential requests to the same host is to reuse a single persistent TCP connection to this host - and this is exactly what you see.处理对同一主机的许多连续请求的最有效方法是重用与该主机的单个持久 TCP 连接 - 这正是您所看到的。 More connections are needed if requests to the same host are sent in parallel instead sequential or if requests are sent to multiple hosts.如果对同一主机的请求是并行而不是顺序发送的,或者如果请求被发送到多个主机,则需要更多的连接。

To check the latter just do requests to example.com and example.org - you'll end up with two entries in the connection cache, one for example.com and one for example.org.要检查后者,只需向 example.com 和 example.org 发出请求 - 您最终会在连接缓存中得到两个条目,一个是 example.com,一个是 example.org。

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

相关问题 如何在Perl中使用LWP :: UserAgent获取HTTP响应的主体? - How can I get the body of an HTTP response using LWP::UserAgent in Perl? 如何让LWP :: UserAgent看起来像另一个浏览器? - How can I make LWP::UserAgent look like another browser? 如何使LWP :: UserAgent看起来已安装Flash? - How can I make LWP::UserAgent appear to have Flash installed? Perl LWP:UserAgent如何添加标题? - Perl LWP:UserAgent how to I add headers? 如果我不能直接替换应用程序代码中的$ ua,我如何使用Test :: LWP :: UserAgent? - How can I use Test::LWP::UserAgent if I cannot replace the $ua in the app code directly? 如何使用LWP :: UserAgent接受gzip压缩内容? - How can I accept gzip-compressed content using LWP::UserAgent? 如何使用Perl的LWP :: UserAgent访问Closure JavaScript压缩程序? - How can I access Closure JavaScript minifier using Perl's LWP::UserAgent? 如何使用LWP :: UserAgent作为附件重定向7z压缩的内容 - How can I redirect 7z-compressed content using LWP::UserAgent as an attachment 如何使用Perl和仅LWP :: UserAgent更新我的Twitter状态? - How can I update my Twitter status with Perl and only LWP::UserAgent? 如何使用Perl的LWP :: UserAgent获取具有不同查询字符串的相同URL? - How can I fetch the same URL with different query string with Perl's LWP::UserAgent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM