并发编程
并发编程: https://www.processon.com/mindmap/5f636bac0791295dccc46f28
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor import requests import os def get_page(url): print("<进程%s> get %s" %(os.getpid(),url)) respone=requests.get(url) if respone.status_code == 200: return {"url":url,"text":respone.text} def parse_page(res): res=res.result() print("<进程%s> parse %s" %(os.getpid(),res["url"])) parse_res="url:<%s> size:[%s] " %(res["url"],len(res["text"])) with open("db.txt","a") as f: f.write(parse_res) if __name__ == "__main__": urls=[ "https://www.baidu.com", "https://www.python.org", "https://www.openstack.org", "https://help.github.com/", "http://www.sina.com.cn/" ] p=ProcessPoolExecutor(3) for url in urls: p.submit(get_page,url).add_done_callback(parse_page) #parse_page拿到的是一个future对象obj,需要用obj.result()拿到结果