鍗忕▼鐩稿叧
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍗忕▼鐩稿叧相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%af%b9%e8%b1%a1' title='瀵硅薄'>瀵硅薄
round .com query 鎬濇兂 object eps blocking 閬囧埌---鎭㈠鍐呭寮€濮?--
鎬濇兂
涓昏鐨勬€濇兂:
濡傛灉涓€涓彉閲忚嚜宸辨湁鏌愮鏂规硶,鑰屼綘鎯冲湪涓嶆敼鍙樿皟鐢ㄦ柟寮忕殑鍓嶆彁涓?甯屾湜鍙互鐐瑰嚭瀹冩湰韬笉瀛樺湪鐨勬柟娉?灏辫鎯冲埌鐢? 绫? 灏佽鐨勬€濇兂 , 灏嗚繖涓彉閲?鏀瑰彉涓轰竴涓被鐨勫璞?鍦ㄧ被涓鍔犱綘闇€瑕佺殑鏂规硶
渚嬪:
# lst1= [1,2,3,4]#鏈塧ppend 鐨勫姛鑳?br># lst2= [1,2,3,4]#鏈塧ppend 鐨勫姛鑳?br># lst3= [1,2,3,4]#鏈塧ppend 鐨勫姛鑳?br># lst4= [1,2,3,4]#鏈塧ppend 鐨勫姛鑳?br># l = [lst1,lst2,lst3,lst4]
# for i in l:
# i.append(6)#寰堟槑鏄鹃兘鍙互鎵ц,
# # i.eat() 鏄庢樉鐩存帴椋樼孩浜?鎬庢牱鍦ㄦ垜涓嶆敼鍙樿皟鐢ㄦ柟寮忕殑鍓嶆彁涓?鍙互灏嗚鍔熻兘瀹炵幇:
class Foo(object): def __init__(self,lst,eat): self.eat = eat self.lst = lst def append(self,n): print(n+1) self.lst.append(3) lst1= [1,2,3,4] #鏈塧ppend 鐨勫姛鑳?/span> lst2= [1,2,3,4] #鏈塧ppend 鐨勫姛鑳?/span> lst3= [1,2,3,4] #鏈塧ppend 鐨勫姛鑳?/span> lst4= [1,2,3,4] #鏈塧ppend 鐨勫姛鑳?/span> lst1_obj = Foo(lst1,"鑻规灉") lst2_obj = Foo(lst2,"姗樺瓙") lst3_obj = Foo(lst3,"xiangjiao") lst4_obj = Foo(lst4,"jiaozi") l = [lst1_obj,lst2_obj,lst3_obj,lst4_obj] for i in l: i.append(3)#寰堟槑鏄鹃兘鍙互鎵ц, print(i.eat) #鏄庢樉鐩存帴椋樼孩浜?鎬庢牱鍦ㄦ垜涓嶆敼鍙樿皟鐢ㄦ柟寮忕殑鍓嶆彁涓?鍙互灏嗚鍔熻兘瀹炵幇:
# 浣犲啓鐨勪唬鐮侊細7000w v = [ [11,22], # 姣忎釜閮芥湁涓€涓猘ppend鏂规硶 [22,33], # 姣忎釜閮芥湁涓€涓猘ppend鏂规硶 [33,44], # 姣忎釜閮芥湁涓€涓猘ppend鏂规硶 ] # 鐜嬫€濊仾 for item in v: print(item.append) 涔嬪悗锛? class Foo(object): def __init__(self,data,girl): self.row = data self.girl = girl def append(self,item): self.row.append(item) v = [ Foo([11,22],鈥?/span>闆ⅷ鈥?/span>), # 姣忎釜閮芥湁涓€涓猘ppend鏂规硶 Foo([22,33],鈥?/span>鍐扮硸鈥?/span>), # 姣忎釜閮芥湁涓€涓猘ppend鏂规硶 Foo([33,44],鈥?/span>绯栧疂鈥?/span>), # 姣忎釜閮芥湁涓€涓猘ppend鏂规硶 ] for item in v: print(item.append) item.girl
涓€ IO澶氳矾澶嶇敤
1 Io澶氳矾澶嶇敤鐨勪綔鐢?鐩戞祴澶氫釜socket 鏄惁鍙戠敓鍙樺寲(鏄惁宸茬粡鍙戠敓鎴愬姛杩炴帴,鏄惁璇诲彇鍒版暟鎹?
鎿嶄綔绯荤粺鐩戞祴socket鏄惁鍙戠敓鍙樺寲,鏈変笁绉嶆ā寮?
select: 鏈€澶氱洃鍚?024,寰幆鐩戞祴
poll;涓嶉檺鍒剁洃鍚瑂ocket涓暟,杩樻槸寰幆鐩戞祴
epoll:涓嶉檺鍒剁洃鍚釜鏁?鍥炶皟鏂瑰紡鐩戞祴(杈圭紭瑙﹀彂)
2 寮傛闈為樆濉?
闃诲:涓嶇瓑寰?
姣斿鍒涘缓socket瀵规煇涓湴鍧€ 杩涜 connect 鑾峰彇鎺ュ彈鏁版嵁,閮戒細榛樿闃诲
銆€銆€銆€銆€銆€銆€濡傛灉璁剧疆setblocking(False),浠ヤ笂涓や釜杩囩▼灏变笉鍐嶇瓑寰咃紝浣嗘槸浼氭姤BlockingIOError鐨勯敊璇紝鍙鎹曡幏灏辫
寮傛:锛岄€氱煡锛屾墽琛屽畬鎴愪箣鍚庤嚜鍔ㄦ墽琛屽洖璋冨嚱鏁版垨鑷姩鎵ц鏌愪簺鎿嶄綔锛堥€氱煡锛夈€?/p>
銆€銆€銆€銆€銆€銆€姣斿鍋氱埇铏腑鍚戞煇涓湴鍧€baidu.com鍙戦€佽姹傦紝褰撹姹傛墽琛屽畬鎴愪箣鍚庤嚜鎵ц鍥炶皟鍑芥暟銆?/p>
3 鍚屾 闃诲
鍚屾 ;鎸夐『搴忔墽琛?/p>
闃诲:绛?/p>
鏅€氱殑鍗曡繘绋嬪崟绾跨▼
import socket import requests # 鏂瑰紡涓€ ret = requests.get(鈥?/span>https://www.baidu.com/s?wd=alex鈥?/span>) # 鏂瑰紡浜?/span> client = socket.socket() # 鐧惧害鍒涘缓杩炴帴: 闃诲 client.connect((鈥?/span>www.baidu.com鈥?/span>,80)) # 闂櫨搴︽垜瑕佷粈涔堬紵 client.sendall(b鈥?/span>GET /s?wd=alex HTTP/1.0 host:www.baidu.com 鈥?/span>) # 鎴戠瓑鐫€鎺ユ敹鐧惧害缁欐垜鐨勫洖澶?/span> chunk_list = [] while True: chunk = client.recv(8096) if not chunk: break chunk_list.append(chunk) body = b鈥樷€?/span>.join(chunk_list) print(body.decode(鈥?/span>utf-8鈥?/span>))
瑙e喅骞跺彂----鍗曠嚎绋?/span>
for item in key_list: ret = requests.get(鈥?/span>https://www.baidu.com/s?wd=%s鈥?/span> %item) # 鏂瑰紡浜?/span> def get_data(key): # 鏂瑰紡浜?/span> client = socket.socket() # 鐧惧害鍒涘缓杩炴帴: 闃诲 client.connect((鈥?/span>www.baidu.com鈥?/span>,80)) # 闂櫨搴︽垜瑕佷粈涔堬紵 client.sendall(b鈥?/span>GET /s?wd=alex HTTP/1.0 host:www.baidu.com 鈥?/span>) # 鎴戠瓑鐫€鎺ユ敹鐧惧害缁欐垜鐨勫洖澶?/span> chunk_list = [] while True: chunk = client.recv(8096) if not chunk: break chunk_list.append(chunk) body = b鈥樷€?/span>.join(chunk_list) print(body.decode(鈥?/span>utf-8鈥?/span>)) key_list = [鈥?/span>alex鈥?/span>,鈥?/span>db鈥?/span>,鈥?/span>sb鈥?/span>] for item in key_list: get_data(item)
瑙e喅骞跺彂-------澶氱嚎绋?/span>
import threading key_list = [鈥?/span>alex鈥?/span>,鈥?/span>db鈥?/span>,鈥?/span>sb鈥?/span>] for item in key_list: t = threading.Thread(target=get_data,args=(item,)) t.start()
瑙e喅骞跺彂-----鍗曠嚎绋?+io澶氳矾澶嶇敤
鍏堜簡瑙d竴涓媠etblocking
import socket client = socket.socket() client.setblocking(True) # 灏嗗師鏉ラ樆濉炵殑浣嶇疆鍙樻垚闈為樆濉烇紙鎶ラ敊锛?鏀规垚 false鍗冲彉鎴愰樆濉炵殑浜?/span> # 鐧惧害鍒涘缓杩炴帴: 闃诲 try: client.connect((鈥?/span>www.baidu.com鈥?/span>,80)) # 鎵ц浜嗕絾鎶ラ敊浜?/span> except BlockingIOError as e: pass # 妫€娴嬪埌宸茬粡杩炴帴鎴愬姛 # 闂櫨搴︽垜瑕佷粈涔堬紵 client.sendall(b鈥?/span>GET /s?wd=alex HTTP/1.0 host:www.baidu.com 鈥?/span>) # 鎴戠瓑鐫€鎺ユ敹鐧惧害缁欐垜鐨勫洖澶?/span> chunk_list = [] while True: chunk = client.recv(8096) # 灏嗗師鏉ラ樆濉炵殑浣嶇疆鍙樻垚闈為樆濉烇紙鎶ラ敊锛?/span> if not chunk: break chunk_list.append(chunk) body = b鈥樷€?/span>.join(chunk_list) print(body.decode(鈥?/span>utf-8鈥?/span>))
鏅€氱増鏈?--鍗曠嚎绋?nbsp; +io 鍩轰簬浜嬩欢寰幆瀹炵幇鐨勫紓姝ラ潪闃诲妗嗘灦
import socket import select client1 = socket.socket() client1.setblocking(False) # 鐧惧害鍒涘缓杩炴帴: 闈為樆濉?/span> try: client1.connect((鈥?/span>www.baidu.com鈥?/span>,80)) except BlockingIOError as e: pass client2 = socket.socket() client2.setblocking(False) # 鐧惧害鍒涘缓杩炴帴: 闈為樆濉?/span> try: client2.connect((鈥?/span>www.sogou.com鈥?/span>,80)) except BlockingIOError as e: pass client3 = socket.socket() client3.setblocking(False) # 鐧惧害鍒涘缓杩炴帴: 闈為樆濉?/span> try: client3.connect((鈥?/span>www.oldboyedu.com鈥?/span>,80)) except BlockingIOError as e: pass socket_list = [client1,client2,client3] conn_list = [client1,client2,client3] while True: rlist,wlist,elist = select.select(socket_list,conn_list,[],0.005) # wlist涓〃绀哄凡缁忚繛鎺ユ垚鍔熺殑socket瀵硅薄 for sk in wlist: if sk == client1: sk.sendall(b鈥?/span>GET /s?wd=alex HTTP/1.0 host:www.baidu.com 鈥?/span>) elif sk==client2: sk.sendall(b鈥?/span>GET /web?query=fdf HTTP/1.0 host:www.sogou.com 鈥?/span>) else: sk.sendall(b鈥?/span>GET /s?wd=alex HTTP/1.0 host:www.oldboyedu.com 鈥?/span>) conn_list.remove(sk) for sk in rlist: chunk_list = [] while True: try: chunk = sk.recv(8096) if not chunk: break chunk_list.append(chunk) except BlockingIOError as e: break body = b鈥樷€?/span>.join(chunk_list) # print(body.decode(鈥榰tf-8鈥?) print(鈥?/span>------------>鈥?/span>,body) sk.close() socket_list.remove(sk) if not socket_list: break
楂樼骇鐗堟湰鐨?nbsp; 骞跺彂 -----鍗曠嚎绋?Io
import socket import select class Req(object): def __init__(self,sk,func): self.sock = sk self.func = func def fileno(self): return self.sock.fileno() class Nb(object): def __init__(self): self.conn_list = [] self.socket_list = [] def add(self,url,func): client = socket.socket() client.setblocking(False) # 闈為樆濉?/span> try: client.connect((url, 80)) except BlockingIOError as e: pass obj = Req(client,func) self.conn_list.append(obj) self.socket_list.append(obj) def run(self): while True: rlist,wlist,elist = select.select(self.socket_list,self.conn_list,[],0.005) # wlist涓〃绀哄凡缁忚繛鎺ユ垚鍔熺殑req瀵硅薄 for sk in wlist: # 鍙戠敓鍙樻崲鐨剅eq瀵硅薄 sk.sock.sendall(b鈥?/span>GET /s?wd=alex HTTP/1.0 host:www.baidu.com 鈥?/span>) self.conn_list.remove(sk) for sk in rlist: chunk_list = [] while True: try: chunk = sk.sock.recv(8096) if not chunk: break chunk_list.append(chunk) except BlockingIOError as e: break body = b鈥樷€?/span>.join(chunk_list) # print(body.decode(鈥榰tf-8鈥?) sk.func(body) sk.sock.close() self.socket_list.remove(sk) if not self.socket_list: break def baidu_repsonse(body): print(鈥?/span>鐧惧害涓嬭浇缁撴灉锛?/span>鈥?/span>,body) def sogou_repsonse(body): print(鈥?/span>鎼滅嫍涓嬭浇缁撴灉锛?/span>鈥?/span>, body) def oldboyedu_repsonse(body): print(鈥?/span>鑰佺敺瀛╀笅杞界粨鏋滐細鈥?/span>, body) t1 = Nb() t1.add(鈥?/span>www.baidu.com鈥?/span>,baidu_repsonse) t1.add(鈥?/span>www.sogou.com鈥?/span>,sogou_repsonse) t1.add(鈥?/span>www.oldboyedu.com鈥?/span>,oldboyedu_repsonse) t1.run()
浜?nbsp; 鍗忕▼ 鍗忕▼涓嶆槸鐪熷疄瀛樺湪鐨?nbsp; 鏄▼搴忓憳涓轰簡鎻愰珮骞跺彂鏁堢巼,寮€鍒涚殑
鍗忕▼ 杩涚▼锛屾搷浣滅郴缁熶腑瀛樺湪锛? 绾跨▼锛屾搷浣滅郴缁熶腑瀛樺湪锛? 鍗忕▼锛屾槸鐢辩▼搴忓憳鍒涢€犲嚭鏉ョ殑涓€涓笉鏄湡瀹炲瓨鍦ㄧ殑涓滆タ锛? 鍗忕▼锛氭槸寰嚎绋嬶紝瀵逛竴涓嚎绋嬭繘绋嬪垎鐗囷紝浣垮緱绾跨▼鍦ㄤ唬鐮佸潡涔嬮棿杩涜鏉ュ洖鍒囨崲鎵ц锛岃€屼笉鏄湪鍘熸潵閫愯鎵ц銆? 娉ㄦ剰锛氬崟绾殑鍗忕▼鏃犵敤 鍗忕▼ + 閬囧埌IO灏卞垏鎹?=> 鐗涢€艰捣鏉ヤ簡 pip3 install gevent 鎬荤粨锛? 1. 浠€涔堟槸鍗忕▼锛? 鍗忕▼涔熷彲浠ョО涓衡€滃井绾跨▼鈥濓紝灏辨槸寮€鍙戣€呮帶鍒剁嚎绋嬫墽琛屾祦绋嬶紝鎺у埗鍏堟墽琛屾煇娈典唬鐮佺劧鍚庡啀鍒囨崲鍒板彟澶栧嚱鎵ц浠g爜...鏉ュ洖鍒囨崲銆? 2. 鍗忕▼鍙互鎻愰珮骞跺彂鍚楋紵 鍗忕▼鑷繁鏈韩鏃犳硶瀹炵幇骞跺彂锛堢敋鑷虫€ц兘浼氶檷浣庯級銆? 鍗忕▼+IO鍒囨崲鎬ц兘鎻愰珮銆? 3. 杩涚▼銆佺嚎绋嬨€佸崗绋嬬殑鍖哄埆锛? 4. 鍗曠嚎绋嬫彁渚涘苟鍙戯細 - 鍗忕▼+IO鍒囨崲锛歡event - 鍩轰簬浜嬩欢寰幆鐨勫紓姝ラ潪闃诲妗嗘灦锛歍wisted
import greenlet def f1(): print(11) gr2.switch() print(22) gr2.switch() def f2(): print(33) gr1.switch() print(44) # 鍗忕▼ gr1 gr1 = greenlet.greenlet(f1) # 鍗忕▼ gr2 gr2 = greenlet.greenlet(f2) gr1.switch()
from gevent import monkey monkey.patch_all() # 浠ュ悗浠g爜涓亣鍒癐O閮戒細鑷姩鎵цgreenlet鐨剆witch杩涜鍒囨崲 import requests import gevent def get_page1(url): ret = requests.get(url) print(url,ret.content) def get_page2(url): ret = requests.get(url) print(url,ret.content) def get_page3(url): ret = requests.get(url) print(url,ret.content) gevent.joinall([ gevent.spawn(get_page1, 鈥?/span>https://www.python.org/鈥?/span>), # 鍗忕▼1 gevent.spawn(get_page2, 鈥?/span>https://www.yahoo.com/鈥?/span>), # 鍗忕▼2 gevent.spawn(get_page3, 鈥?/span>https://github.com/鈥?/span>), # 鍗忕▼3 ])
def f1(): print(11) yield print(22) yield print(33) def f2(): print(55) yield print(66) yield print(77) v1 = f1() v2 = f2() next(v1) # v1.send(None) next(v2) # v1.send(None) next(v1) # v1.send(None) next(v2) # v1.send(None) next(v1) # v1.send(None) next(v2) # v1.send(None)
import socket import select class Req(object): def __init__(self,sk,func): self.sock = sk self.func = func def fileno(self): return self.sock.fileno() class Nb(object): def __init__(self): self.conn_list = [] self.socket_list = [] def add(self,url,func): client = socket.socket() client.setblocking(False) # 闈為樆濉?/span> try: client.connect((url, 80)) except BlockingIOError as e: pass obj = Req(client,func) self.conn_list.append(obj) self.socket_list.append(obj) def run(self): while True: rlist,wlist,elist = select.select(self.socket_list,self.conn_list,[],0.005) # wlist涓〃绀哄凡缁忚繛鎺ユ垚鍔熺殑req瀵硅薄 for sk in wlist: # 鍙戠敓鍙樻崲鐨剅eq瀵硅薄 sk.sock.sendall(b鈥?/span>GET /s?wd=alex HTTP/1.0 host:www.baidu.com 鈥?/span>) self.conn_list.remove(sk) for sk in rlist: chunk_list = [] while True: try: chunk = sk.sock.recv(8096) if not chunk: break chunk_list.append(chunk) except BlockingIOError as e: break body = b鈥樷€?/span>.join(chunk_list) # print(body.decode(鈥榰tf-8鈥?) sk.func(body) sk.sock.close() self.socket_list.remove(sk) if not self.socket_list: break
from emma import Nb def baidu_repsonse(body): print(鈥?/span>鐧惧害涓嬭浇缁撴灉锛?/span>鈥?/span>,body) def sogou_repsonse(body): print(鈥?/span>鎼滅嫍涓嬭浇缁撴灉锛?/span>鈥?/span>, body) def oldboyedu_repsonse(body): print(鈥?/span>鑰佺敺瀛╀笅杞界粨鏋滐細鈥?/span>, body) t1 = Nb() t1.add(鈥?/span>www.baidu.com鈥?/span>,baidu_repsonse) t1.add(鈥?/span>www.sogou.com鈥?/span>,sogou_repsonse) t1.add(鈥?/span>www.oldboyedu.com鈥?/span>,oldboyedu_repsonse) t1.run()
---鎭㈠鍐呭缁撴潫---
以上是关于鍗忕▼鐩稿叧的主要内容,如果未能解决你的问题,请参考以下文章