python线程池的实现实例
直接上代码:
代码如下:
# -*- coding: utf-8 -*- import Queue import threading import urllib import urllib2 import os def down(url,n): print 'item '+str(n)+' start ' filename=urllib2.unquote(url).decode('utf8').split('/')[-1] urllib.urlretrieve(url, filename) print 'item '+str(n)+' finish ' def worker(): while True: i = q.get() url=i[0] n=i[1] down(url,n) q.task_done() if __name__=="__main__": num_worker_threads=100 f=open('url.txt') l=f.readlines() q = Queue.Queue() for i in range(num_worker_threads): t = threading.Thread(target=worker) t.daemon = True t.start() for i in range(0,len(l)): q.put((l[i],i)) q.join()
相关推荐
meylovezn 2020-05-30
cenylon 2020-04-29
georgeandgeorge 2020-01-02
数据猿 2019-05-10
liuxiaohua 2019-06-25
jibkfv 2019-06-21
fengling 2019-03-19
zhglinux 2017-01-10
pythonwangjunji 2018-04-27
chouliqingke 2016-11-06
CodeAsWind 2010-02-26
周小董 2018-01-06
AndroidAmelia 2017-11-17
wgPython 2015-06-30
wlpython 2019-04-18