day 32-2 练习
基于 requests 实现简单线程池的运用
from concurrent.futures import ThreadPoolExecutor import requests def gets(url): print('正在获取 %s' % url) info = requests.get(url) num = len(info.text) return {'url': url, 'num': num} def ret(info): info = info.result() print('%s 的长度为 %s' % (info['url'], info['num'])) if __name__ == '__main__': url = ['https://www.cnblogs.com/ysging/p/12339912.html', 'https://www.cnblogs.com/ysging/p/12329308.html', 'https://www.cnblogs.com/ysging/p/12319763.html'] t = ThreadPoolExecutor(2) for i in url: t.submit(gets, i).add_done_callback(ret) # 执行结果 # 正在获取 https://www.cnblogs.com/ysging/p/12339912.html # 正在获取 https://www.cnblogs.com/ysging/p/12329308.html # https://www.cnblogs.com/ysging/p/12329308.html 的长度为 41115 # 正在获取 https://www.cnblogs.com/ysging/p/12319763.html # https://www.cnblogs.com/ysging/p/12339912.html 的长度为 53267 # https://www.cnblogs.com/ysging/p/12319763.html 的长度为 30937