Skip to content

Instantly share code, notes, and snippets.

@lucafaggianelli
Created August 27, 2014 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucafaggianelli/29b0906b4b458bd845f7 to your computer and use it in GitHub Desktop.
Save lucafaggianelli/29b0906b4b458bd845f7 to your computer and use it in GitHub Desktop.
Download many files in parallel
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