如何确定 LWP::Simple 的 getstore() 的下载速度和数量?
使用perl模块时 LWP::Simple,是否有一种简单的方法可以确定单次 getstore() 调用下载的速度和数量?这对于观察大文件下载的状态很有用。
在我的脑海中,一种方法是:
- 存储当前时间 (time0)
- 在新进程中运行 getstore
- 轮询已知的目标文件
- 下载的数量将是当前文件大小(大小)
- 下载速度为(大小 / 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:
- store the current time (time0)
- run getstore in a new process
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用
LWP::Simple
,而是使用 LWP::UserAgent< /a> 直接。首先,看看 LWP::Simple::getstore 如何初始化 $ua 并调用请求。您需要调用$ua->add_handler
来指定response_data
处理程序来执行您想要的操作;默认情况下(至少对于 HTTP 协议)LWP::UserAgent
将读取最多 4Kb 块并为每个块调用response_data
处理程序,但您可以建议不同的处理程序请求方法参数中的大小。如果您想区分标头数据和将存储在文件中的实际数据,或者在存在重定向时执行一些特殊操作,您可能还想指定其他处理程序。
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 aresponse_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 theresponse_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.
除非您有其他要求(例如在下载过程中查看速率和大小),否则您概述的步骤是最容易考虑和实施的。
您可以在 LWP::Simple 中导出底层用户代理对象。如果您只想一次性观看下载,您可以设置用户代理的
show_progress
位:要完成更多工作,您可以使用 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>:
在该子例程中,您可以读取数据并对其执行任何您喜欢的操作,包括保留计时器。得到答案后,您可以删除那段代码并返回到
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:To do more work, you can use LWP::Simple and still do the same thing ysth suggests:
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.