使用Python和urllib2下载低优先级文件

发布于 2024-11-18 07:37:32 字数 363 浏览 1 评论 0原文

我正在编写一个简单的工具来保留我复制的每张图像(图像的 URL) 我正在使用 pythoncom 和 pyhook 来捕获键盘“复制”组合。 这里的问题是,当图像开始下载时,它会导致速度大幅下降,因此我必须等待 0.5-2 秒才能完成脚本。代码如下:

response = urllib2.urlopen (URL) 
image_file = response.read()
file = open (path + filename, 'wb')
file.write (image_file)
file.close()

那么,问题是 - 有没有办法消除这种延迟?

I'm writing simple tool to keep every image that I've copied (URL of image)
I'm using pythoncom and pyhook to catch keyboard "Copy" combination.
The problem here is that when Image starts downloading it causes major slowdown, so I have to wait for 0.5-2 seconds for script to finish. Here's the code:

response = urllib2.urlopen (URL) 
image_file = response.read()
file = open (path + filename, 'wb')
file.write (image_file)
file.close()

So, the question is - is there a way to get rid of this delay?

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

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

发布评论

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

评论(1

手心的海 2024-11-25 07:37:32

目前的问题(未经编辑)并不是那么清楚。但我的印象是你的问题是你必须在处理图像之前从某个地方下载图像。为了解决这个问题,我将使用以下两种方法之一:

  • 抢先下载。您开始下载用户最有可能希望在程序空闲时间下载的图像,以便当/如果用户最终选择下载它时,您只需从预先下载的缓存中“选择它”即可。
  • 您将获得一个异步进程来为您下载它。这种情况在程序逻辑方面更难以管理。您应该查看多处理模块文档。该模块自 2.6 版本起成为标准库的一部分。

The question in its current state (unedited) is not really that clear. But I am under the impression your problem is the fact you have to download the image from somewhere before processing it. To get around that, I would use either of the two:

  • Pre-emptive downloading. You start download the image(s) that is most likely the user will wish to download during program's idle time, so that when/if the user eventually choose to download it, you simply "pick it" from the pre-downloaded cache.
  • You get an asynchronous process to download it for you. This case is more tricky to manage in terms of program logic. You should look at the multiprocessing module documentation. This module is part of the standard lib since version 2.6.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文