在 Python 中跟踪文件加载进度
我使用的许多模块都会将整个文件导入内存或在处理文件时将文件的内容滴入其中。 我想知道是否有任何方法可以跟踪这种加载进度? 可能是一个需要回调的包装类?
A lot of modules I use import entire files into memory or trickle a file's contents in while they process it. I'm wondering if there's any way to track this sort of loading progress? Possibly a wrapper class that takes a callback?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会通过确定文件的大小,然后简单地将总数除以读取的字节数来实现这一点。 像这样:
编辑:我知道这不是最好的代码,但我只是想展示这个概念。
I would do by this by determining the size of the file, and then simply dividing the total by the number of bytes read. Like this:
Edit: I know it isn't the best code, but I just wanted to show the concept.
如果您实际上是指“导入”(而不是“读取”),那么您可以覆盖导入模块定义。 您可以添加计时功能。
请参阅 imp 模块。
如果您的意思是“读取”,那么您可以使用自己的类似文件的包装器简单地包装 Python 文件。 文件不会公开太多方法。 您可以覆盖有趣的部分来获取计时数据。
If you actually mean "import" (not "read") then you can override the import module definitions. You can add timing capabilities.
See the imp module.
If you mean "read", then you can trivially wrap Python files with your own file-like wrapper. Files don't expose too many methods. You can override the interesting ones to get timing data.