1.管道
2.數據共享 Manager
3.進程池
1.map
1.返回全部結果列表
2.異步 自帶close和join
2.apply
1.同步
3.apply_async
1.異步:func被註冊進入一個進程,程序繼續執行
2.返回值爲一個相關對象
1.線程與進程
1.進程——最小的內存分配單位
2.線程——操做系統調度的最小單位
3.線程被CPU執行
4.進程至少含有一個線程
5.起線程須要的時間和空間小於進程
6.多個線程內部有本身的數據棧,這個不能共享
7.全局變量在多個線程間共享
2.起線程的兩種方式
3.進程中存儲:導入的模塊,文件所在的位置,代碼,
主線程:主線程代碼中的變量、對象
子線程:少許必要的數據
4.全局解釋器鎖 GIL
1.鎖的是線程
2.致使cpu利用率不充分,保證數據安全
3.不是python的問題,是Cpython解釋器的問題
4.編譯型語言——編譯後,很大程度避免了數據安全問題
解釋性語言
5.高CPU:計算類——高CPU利用率
高IO:多延時
1.多進程 爬取網頁代碼長度python
from multiprocessing import Pool from urllib.request import urlopen def get(url): ret = urlopen(url) return url, ret.read().decode('utf-8') def handle(args): url, content = args print(url,len(content)) if __name__ == '__main__': url_lst = [ 'http://www.baidu.com', 'https://pan.baidu.com/s/1UwzvTS5o0MB2KQSerNeaWA?fid=809605274381911', 'http://gufenso.coderschool.cn/#' 'http://www.miit.gov.cn/' ] p = Pool() for url in url_lst: p.apply_async(get,args=(url,),callback=handle) p.close() p.join()
2.多進程 爬取電影安全
import re from multiprocessing import Pool from urllib.request import urlopen def get_page(url,pattern): response = urlopen(url).read().decode('utf-8') return pattern,response def parse_page(info): pattern, ret = info res = re.findall(pattern,ret) ret_str = '' for item in res: dic1={ 'index':item[0].strip(), 'title':item[1].strip(), 'actor':item[2].strip(), 'time':item[3].strip(), } dic1_str = str(dic1) + '\n' ret_str = dic1_str + ret_str print(ret_str) return ret_str if __name__ == '__main__': # 生成帶handel網頁列表 url_lst = [] for i in range(0,91,10): url_lst.append('https://maoyan.com/board/4?offset=%d'%i) print(url_lst) # 正則編譯 regex = r'<dd>.*?<i class="board-index board-index-.*?">(\d+)</i>.*?title="(.*?)".*?"star">(.*?)</p>.*?<p class="releasetime">(.*?)</p>.*?</dd>' pattern = re.compile(regex,re.S) # 起進程 res_l = [] p = Pool() for url in url_lst: res = p.apply_async(get_page,args=(url,pattern),callback=parse_page) res_l.append(res) p.close() p.join()
3.線程多線程
import time,os from threading import Thread def func(i): time.sleep(1) global g if g == 100: g = 0 print('in %s,%s'%(i,g)) print('in func %s, pid:%s,g=%s'%(i,os.getpid(),g)) g = 100 t_list = [] for i in range(10): t = Thread(target=func,args=(i,)) t.start() t_list.append(t) for i in t_list:i.join() print(g)
4.多線程 效率app
from multiprocessing import Process from threading import Thread import time def func(n): n + 1 if __name__ == '__main__': p_list = [] t_sta1 = time.time() for i in range(100): p = Process(target=func,args=(i,)) p.start() p_list.append(p) for i in p_list: i.join() t1 = time.time() - t_sta1 t_list = [] t_sta2 = time.time() for i in range(100): t = Thread(target=func,args=(i,)) t.start() t_list.append(t) for i in t_list: i.join() t2 = time.time() - t_sta2 print(t1,t2)
5.threading模塊方法異步
import threading threading.current_thread() threading.get_ident() threading.active_count() # 包括主線程 threading.enumerate()
6.多線程實現socketserversocket
# server import socket from threading import Thread def func(conn): while True: info = input('>>> ') conn.send(info.encode('utf-8')) msg = conn.recv(1204).decode('utf-8') print(msg) sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.listen() while True: conn, addr = sk.accept() t = Thread(target=func,args=(conn,)) t.start() conn.close() sk.close() #client import socket sk = socket.socket() sk.connect(('127.0.0.1',8080)) while 1: msg = sk.recv(1024).decode('utf-8') print(msg) ret = input('>>> ') sk.send(ret.encode('utf-8')) sk.close()