import urllib
import os
import threading
import time
def download(url, filename):
try:
urllib.urlretrieve(url, filename)
except Exception:
os.system('touch ' + filename)
print("Failed Downloading...")
def getphotos():
prefix = 'http://hwcdn.ddstatic.com/fhg/fhg_photos/2011_04_28/11121/roxanna_milan_11121_1-gal-1600-jpg/'
for i in range(20):
filename = "%04d" % (i + 1) + '.jpg'
print filename
download(prefix + filename, filename)
def down_from_file():
itr = 1323
urls = '/Users/chenhaoy/tutorial/piclinks.txt'
maxthread = 30
for url in open(urls):
while threading.activeCount() > maxthread:
print 'threading.activeCount=%d' % (threading.activeCount())
time.sleep(1)
filename = "%05d.jpg" % (itr)
itr += 1
print 'downloading.. %s to %s' % (url, filename)
t = threading.Thread(target=download, args=(url, filename))
t.start()
time.sleep(5)
print 'threading.activeCount=%d' % (threading.activeCount())
print 'add all pic to download list..'
cthread = threading.current_thread()
for t in threading.enumerate():
if t != cthread:
pass #t.terminate()
print 'bye..'
def main():
down_from_file()
if __name__ == '__main__':main()
python multi-thread download files
最新推荐文章于 2022-04-10 15:55:20 发布