python greenlet快速学习(tcy-)
學習協程,原理看鏈接。只要能看懂這一個實例,基本上你就能夠運用greenlet編寫協程。
前提你對yield已經了解,佛則應先學yield.
1.原理
? ? ? ??https://www.cnblogs.com/Security-Darren/p/4167961.html
? ? ? ?https://www.oschina.net/question/3306142_2242565
2.1.yield消費者協程
2.2.greenlet消費者協程
3.yield使用說明
------------------------------------------------------------------------------------------------------------------------------------------------
2.1yield消費者協程
import time
def consumer():
??? send_data2 = ''
??? while True:
??????? receive_data2 = yield send_data2 #receive_data2接收數據;send_data2發送數據
??????????? #向函數發送值時函數會執行 通過 yield拿到消息,處理,又通過 yield把結果傳回;
??????? if not receive_data2:
??????????? return
??????? print('2.客戶:接收(生成數量)= %s ;' % receive_data2)
??????? time.sleep(1)
??????? send_data2 = '¥'+str(100*receive_data2)
??????? print('2.客戶:發送(付款金額)=%s ;' % send_data2)
def produce(c):
??? next(c)? #c.send(None)啟動生成器????????????????? #向前執行到第一條語句,準備接收
??? send_data1 = 0
??? while send_data1 < 3:
??????? send_data1 = send_data1 + 1
??????? print('1.工廠:發送(生產數量)= %s pcs ;' % send_data1)
??????? receive_data1 = c.send(send_data1 )#send_data1發送數據;receive_data1接受數據
??????? print('1.工廠:接收(客戶付款)=: %s ;' % receive_data1)
??? c.close() #produce決定不生產了,通過c.close()關閉 consumer,整個過程結束
#******************************************
if __name__=='__main__':
#??? 調用:
??? c = consumer()
??? produce(c)
?#-----------------------------------------------
#1.工廠:發送(生產數量)= 1 pcs ;
#2.客戶:接收(生成數量)= 1 ;
#2.客戶:發送(付款金額)=¥100 ;
#1.工廠:接收(客戶付款)=: ¥100 ;
#1.工廠:發送(生產數量)= 2 pcs ;
#2.客戶:接收(生成數量)= 2 ;
#2.客戶:發送(付款金額)=¥200 ;
#1.工廠:接收(客戶付款)=: ¥200 ;
#1.工廠:發送(生產數量)= 3 pcs ;
#2.客戶:接收(生成數量)= 3 ;
#2.客戶:發送(付款金額)=¥300 ;
#1.工廠:接收(客戶付款)=: ¥300 ;
-----------------------------------------------------------------------------------------------------------------------------------------------
2.2.greenlet消費者協程
??? # 消費者協程
??? from greenlet import greenlet
???
??? def consumer():
??????? send_data2 = ''
??????? while True:
??????????? receive_data2 = pro.switch(send_data2)
??????????? if receive_data2? is not None:
??????????????? print( '2.receive_data2= %s' % receive_data2 )
??????????????? send_data2 = receive_data2 *10+1
??????????????? print('2.send_data2=',send_data2)
?????
??? def producer(n):
??????? con.switch()
??????? i = 0
??????? while i < n:
??????????? i += 1
??????????? print( '1.send_data1= %s' % i)
??????????? receive_data1 = con.switch(i)
??????????? print('1.receive_data1=',receive_data1)
?????
??? pro = greenlet(producer)
??? con = greenlet(consumer)
??? pro.switch(5)
??? ??
??? # 1.send_data1= 1
??? # 2.receive_data2= 1
??? # 2.send_data2= 11
??? # 1.receive_data1= 11
??? # 1.send_data1= 2
??? # 2.receive_data2= 2
??? # 2.send_data2= 21
??? # 1.receive_data1= 21
??? # 1.send_data1= 3
??? # 2.receive_data2= 3
??? # 2.send_data2= 31
??? # 1.receive_data1= 31
??? # 1.send_data1= 4
??? # 2.receive_data2= 4
??? # 2.send_data2= 41
??? # 1.receive_data1= 41
??? # 1.send_data1= 5
??? # 2.receive_data2= 5
??? # 2.send_data2= 51
??? # 1.receive_data1= 51
??????
? ?----------------------------------------------------------------------------------------------------------------------------------------------------------------------
總結
以上是生活随笔為你收集整理的python greenlet快速学习(tcy-)的全部內容,希望文章能夠幫你解決所遇到的問題。