import os
import re
import urllib.parse
import urllib.request
import threadpool
def download_file(url):
print("Begin download {}".format(url))
urlhandler = urllib.request.urlopen(url)
p = urllib.parse.urlparse(url).path
fname = os.path.basename(re.findall(r"item/(.*?)\W", p, re.S)[0]) + ".html"
with open(fname, 'wb') as f:
while True:
chunk = urlhandler.read(1024)
if not chunk:
break
f.write(chunk)
def main():
urls = [r"http://baike.baidu.com/item/wiki/97755",
r"http://baike.baidu.com/item/Biosteel?fr=Biosteel",
r"http://baike.baidu.com/item/HTML/"]
# 創(chuàng)建大小為2的線程池
pool_size = 2
pool = threadpool.ThreadPool(pool_size)
# 創(chuàng)建工作請(qǐng)求
requests = threadpool.makeRequests(download_file, urls)
[pool.putRequest(req) for req in requests]
# 將具體的請(qǐng)求放入線程池
print("putting request to pool")
pool.putRequest(threadpool.WorkRequest(download_file, args=[r"http://baike.baidu.com/item/duang/16824760",]))
# 處理任務(wù)隊(duì)列中的新請(qǐng)求
pool.poll()
pool.wait()
print("destory all threads before exit...")
pool.dismissedWorkers(pool_size, do_join=True)
if __name__ == '__main__':
main()
代碼如上所示,在Python3.5的運(yùn)行報(bào)錯(cuò):
pool.dismissedWorkers(pool_size, do_join=True)
TypeError: 'list' object is not callable
請(qǐng)問(wèn)一下是哪里錯(cuò)了,應(yīng)該怎么修改呢?
添加回答
舉報(bào)
0/150
提交
取消