#多进程访问6个网址,计算一下一共有多少个字符。用2个子进程来实现。
#encoding=utf-8 from multiprocessing import Process,Queue,Value, Array import requests url= ["http://www.sina.com.cn","http://www.sohu.com","http://www.163.com","http://cn.bing.com","http://www.baidu.com","http://www.iciba.com"] q=Queue() for i in url: q.put(i) def f(count,q): while not q.empty(): #empty判断队列是否为空 url = q.get() r=requests.get(url) count.value +=len(r.text) print(url,len(r.text)) #print(r.text) if __name__ =="__main__": num = Value('i', 0) # 创建一个进程间共享的数字类型,默认值为0 p1 = Process(target = f, args = (num, q)) p2 = Process(target = f, args = (num, q)) p1.start() p2.start() p1.join() p2.join() print(num.value)