from concurrent.futures import ThreadPoolExecutor import requests def task(url): ret = requests.get(url=url) # print(ret.status_code, ret.url) return ret def done(request, *args, **kwargs): # result = request.result() print(result.url, result.status_code, args, kwargs) pool = ThreadPoolExecutor(5) urllist = [ "https://baidu.com", "https://github.com", "http://qq.com", "http://taobao.com", "https://www.360.cn", "http://www.jd.com", ] for url in urllist: v = pool.submit(task, url) v.add_done_callback(done) pool.shutdown(wait=True)
结果:
https://www.qq.com?fromdefault 200 () {} http://www.baidu.com/ 200 () {} https://www.360.cn/ 200 () {} https://www.taobao.com/ 200 () {} https://www.jd.com/ 200 () {} https://github.com/ 200 () {}