如何确定 LWP::Simple 的 getstore() 的下载速度和数量?

发布于 2024-08-12 05:46:48 字数 436 浏览 3 评论 0原文

使用perl模块时 LWP::Simple,是否有一种简单的方法可以确定单次 getstore() 调用下载的速度和数量?这对于观察大文件下载的状态很有用。

在我的脑海中,一种方法是:

  1. 存储当前时间 (time0)
  2. 在新进程中运行 getstore
  3. 轮询已知的目标文件
    • 下载的数量将是当前文件大小(大小)
    • 下载速度为(大小 / current_time - time0)

我想知道是否有更简单的方法。

欢迎其他建议(也许我应该使用不同的模块?)

When using the perl module LWP::Simple, is there a simple way to determine the speed and amount downloaded by a single getstore() invocation? This would be useful for observing the status of large file downloads.

Off the top of my head, one approach would be to:

  1. store the current time (time0)
  2. run getstore in a new process
  3. poll the known destination file
    • the amount downloaded would be the current file size (size)
    • the download speed would (size / current_time - time0)

I'm wondering if there's a simpler way.

Alternative suggestions welcome (perhaps I should use a different module?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄与心 2024-08-19 05:46:48

Instead of using LWP::Simple, use LWP::UserAgent directly. For a starting point, look at how LWP::Simple::getstore initializes a $ua and invokes request. You'll want to call $ua->add_handler to specify a response_data handler to do whatever you want; by default (at least for the HTTP protocol) LWP::UserAgent will be reading up to 4Kb chunks and call the response_data handler for each chunk, but you can suggest a different size in the request method parameters.

You may want to specify other handlers too, if you want to differentiate between header data and actual data that will be stored in the file or do something special if there are redirects.

会发光的星星闪亮亮i 2024-08-19 05:46:48

除非您有其他要求(例如在下载过程中查看速率和大小),否则您概述的步骤是最容易考虑和实施的。

您可以在 LWP::Simple 中导出底层用户代理对象。如果您只想一次性观看下载,您可以设置用户代理的 show_progress 位:

 use LWP::Simple qw($ua getstore);

 $ua->show_progress(1);

 getstore( 
'http://www.theperlreview.com/Issues/subscribers.html',
'subscribers.html'
);

要完成更多工作,您可以使用 LWP::Simple 并仍然执行 < a href="https://stackoverflow.com/questions/1778092/determining-speed-and-amount-downloaded-by-a-single-perl-lwpsimple-getstore-i/1778148#1778148">同样的事情ysth建议< /a>:

 use LWP::Simple qw($ua);

 $ua->response_header( 
      sub { 
          my($response, $ua, $h) = @_; 
          ... 
          }
      );

在该子例程中,您可以读取数据并对其执行任何您喜欢的操作,包括保留计时器。得到答案后,您可以删除那段代码并返回到 getstore

Flavio Poletti 为 The Perl Review,2009 年春季 撰写了“Watching LWP's Activity”,并展示了许多内容使用这种技术。

Unless you have other requirements (such as watching the rate and size during the download), the steps that you outlined are the easiest to think about and implement.

You can export the underlying user-agent object in LWP::Simple. If you just want to watch the download for a one-off, you can set the show_progress bit of the user-agent:

 use LWP::Simple qw($ua getstore);

 $ua->show_progress(1);

 getstore( 
'http://www.theperlreview.com/Issues/subscribers.html',
'subscribers.html'
);

To do more work, you can use LWP::Simple and still do the same thing ysth suggests:

 use LWP::Simple qw($ua);

 $ua->response_header( 
      sub { 
          my($response, $ua, $h) = @_; 
          ... 
          }
      );

In that subroutine, you read the data and do whatever you like with it, including keeping a timer. Once you get your answer, you can delete that bit of code and go back to just getstore.

Flavio Poletti wrote "Watching LWP's Activity" for The Perl Review, Spring 2009 and shows many uses of this technique.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文