Python並發編程:協程-gevent模塊
阿新 • • 發佈:2018-07-12
sea 服務 實現 之前 gree close say bsp decode
一 gevent模塊
Gevent是一個第三方庫,可以輕松通過gevent實現並發同步或異步編程。在gevent中用到的主要模式是Greenlet,它是以C擴展模塊形式接入Python的輕量級協程。Greenlet 全部運行在主程序操作系統進程的內部,但它們被協作式地調度。
# 用法 g1=gevent.spawn(func,1,2,3,x=4,y=5)創建一個協程對象g1,spawn括號內第一個參數是函數名,如eat,後面可以有多個參數,可以是位置實參或關鍵字實參,都是傳給函數eat的 g2 = gevent.spawn(func2) g1.join() # 等待g1結束 g2.join() # 等待g2結束 # 或者上述兩步合作一步:gevent.joinall([g1,g2]) g1.value # 拿到func1的返回值
遇到IO阻塞時會自動切換任務
import gevent def eat(name): print(‘%s eat 1‘ % name) gevent.sleep(2) print(‘%s eat 2‘ % name) def play(name): print(‘%s play 1‘ % name) gevent.sleep(1) print(‘%s play 2‘ % name) g1 = gevent.spawn(eat, ‘mike‘) g2 = gevent.spawn(play, ‘mike‘) g1.join() g2.join() print(‘主‘)
上例gevent.sleep(2)模擬的是gevent可以識別的io阻塞
而time.sleep(2)或其它的阻塞,gevent是不能直接識別的需要用下面一行代碼,打補丁,就可以識別了
from gevent import monkey;monkey.patch_all()必須放到被打補丁者的前面,如time,socket模塊之前
或者我們幹脆記憶成:要用gevent,需要將from gevent import monkey;monkey.patch_all()放到文件的開頭
from gevent import monkey;monkey.patch_all() import gevent import time def eat(name): print(‘%s eat 1‘ % name) time.sleep(2) print(‘%s eat 2‘ % name) def play(name): print(‘%s play 1‘ % name) time.sleep(1) print(‘%s play 2‘ % name) g1 = gevent.spawn(eat, ‘mike‘) g2 = gevent.spawn(play, ‘mike‘) g1.join() g2.join() print(‘主‘)
我們可以用threading.current_thread().getName()來查看每個g1和g2,查看的結果為DummyThread-n,即假線程
二 練習
# 服務端 from gevent import monkey;monkey.patch_all() from socket import * import gevent # 如果不想用monkey.patch_all()打補丁,可以用gevent自帶的socket # from gevent import socket # s = socket.socket() def server(server_ip, port): s = socket(AF_INET, SOCK_STREAM) s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) s.bind((server_ip, port)) s.listen(5) while True: conn, addr = s.accept() gevent.spawn(talk, conn, addr) def talk(conn, addr): try: while True: res = conn.recv(1024) print(‘client %s:%s msg: %s‘ % (addr[0], addr[1], res)) conn.send(res.upper()) except Exception as e: print(e) finally: conn.close() if __name__ == ‘__main__‘: server(‘127.0.0.1‘, 8080)
客戶端
from threading import Thread from socket import * import threading def client(server_ip, port): c = socket(AF_INET, SOCK_STREAM) # 套接字對象一定要加到函數內,即局部名稱空間內,放在函數外則被所有線程共享,則大家公用一個套接字對象,那麽客戶端端口永遠一樣了 c.connect((server_ip, port)) count = 0 while True: c.send((‘%s say hello %s‘ % (threading.current_thread().getName(), count)).encode(‘utf-8‘)) msg = c.recv(1024) print(msg.decode(‘utf-8‘)) count += 1 if __name__ == ‘__main__‘: for i in range(500): t = Thread(target=client, args=(‘127.0.0.1‘, 8080)) t.start()
Python並發編程:協程-gevent模塊