python_线程池

≡放荡痞女 提交于 2019-12-06 05:34:37

一、线程池

实例:

 1 import threadpool
 2 import requests,time,threading
 3 from hashlib import md5
 4 def down_load_pic(url):
 5     req = requests.get(url)
 6     m = md5(url.encode())
 7     with open( m.hexdigest()+'.png','wb') as fw:
 8         fw.write(req.content)
 9 
10 url_list = ['http://www.nnzhp.cn/wp-content/uploads/2019/10/f410afea8b23fa401505a1449a41a133.png',
11             'http://www.nnzhp.cn/wp-content/uploads/2019/11/481b5135e75c764b32b224c5650a8df5.png',
12             'http://www.nnzhp.cn/wp-content/uploads/2019/11/b23755cdea210cfec903333c5cce6895.png',
13             'http://www.nnzhp.cn/wp-content/uploads/2019/11/542824dde1dbd29ec61ad5ea867ef245.png']
14 
15 pool = threadpool.ThreadPool(20)   #实例化一个线程,启动20个线程
16 reqs = threadpool.makeRequests(down_load_pic,url_list)  #自动分配数据
17 for req in reqs:
18     pool.putRequest(req)
19 print(threading.activeCount())  #查看当前线程数
20 print(threading.current_thread())  #查看当前线程
21 pool.wait()  #等待
22 print('end')

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!