Created
August 27, 2014 14:48
-
-
Save lucafaggianelli/29b0906b4b458bd845f7 to your computer and use it in GitHub Desktop.
Download many files in parallel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
import urllib2 | |
from multiprocessing import Pool | |
BASE = 'http://images.philips.com/is/image/PhilipsConsumer/HD8761_01-P3D-global-{0:0>3}?$pngsmall$' | |
def millis(): | |
return int(round(time.time() * 1000)) | |
def http_get(i): | |
start_time = millis() | |
try: | |
url = BASE.format(i) | |
f = urllib2.urlopen(url, timeout=5) | |
print url + " took " + str(millis() - start_time) + " ms" | |
with open('imgs/%d.png'%i, "wb") as local_file: | |
local_file.write(f.read()) | |
except urllib2.HTTPError, e: | |
print "HTTP Error:", e.code, url | |
except urllib2.URLError, e: | |
print "URL Error:", e.reason, url | |
if __name__ == '__main__': | |
pool = Pool(processes=4) | |
pool.map(http_get, range(1,73)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment