Download file with low priority using Python and urllib2
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 wa开发者_如何学编程it 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?
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.
精彩评论