显式关闭由LWP :: Simple建立的连接

时间:2017-07-20 18:10:45

标签: perl tcp lwp

我有一个使用getprint的Perl脚本 LWP::Simple 获取一些JSON数据。

我们看到两台服务器上的TCP连接仍然保持打开状态。

我想确保Perl在getprint完成时显式关闭连接。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

虽然已经指出LWP :: Simple不应该保持连接未打开,但我建议使用LWP::UserAgent。然后,您可以简单地让对象超出范围:

{
    # Set the timeout to some value you find reasonable
    my $ua = LWP::UserAgent->new( timeout => 30 );
    # open connection, do what you wanted to use it for
    # ...
    # will run out of scope now. This may not be necessary,
    # but now you can be quite certain the connection is dropped.

}

oo-interface还允许(imo)更简单的配置,如超时等。

相关问题