本文主要是介绍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
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.协程介绍 2018/8/15-----------------------------------------------------------------
'''
协程:是单线程下的并发,又称微线程,纤程。英文名Coroutine。
缺点:#1. 无法利用多核,可开启多进程,进程内开启多线程,线程内开启协程#2. 协程是单线程,一旦协程出现阻塞会阻塞整个线程
总结:必须在只有一个单线程里实现并发修改共享数据不需加锁用户程序里自己保存多个控制流的上下文栈
附加:一个协程遇到IO操作自动切换到其它协程(如检测IO,yield、greenlet都无法实现,就用gevent模块(select机制)说明:发生器也成为协同程序,这是一种更普遍的子程序形式。子程序在一点输入并在另一点(函数顶部和一个 return语句)退出,可以在许多不同的点(yield语句)输入,退出和恢复协程。使用yield实现协程:
---------------------------------------------------------------------------
#**************************************************************************
'''
import timedef people(name):print("Ready buy product ....")while True:receive_data2= yield # 程序暂停等待nextprint("{} is eating {}...".format(name,receive_data2))def factory():boy.__next__() # 实例boy启动yieldgirl.__next__() # 实例girl启动yieldi = 0while i<5:i += 1print("productNO: {}".format(i))boy.send(i) # 向yield发送数据,yield恢复,并自动执行nextgirl.send(i) # 向yield发送数据,yield恢复,并自动执行nexttime.sleep(1)
#**************************************************************************
boy = people("Tom") # 创建实例boy
girl = people("Alice") # 创建实例girl
product = factory() # 启动函数p
#**************************************************************************
#>>>
#Ready buy product ....
#Ready buy product ....
#productNO: 1
#Tom is eating 1...
#Alice is eating 1...
#productNO: 2
#Tom is eating 2...
#Alice is eating 2...
#productNO: 3
#Tom is eating 3...
#Alice is eating 3...
#productNO: 4
#Tom is eating 4...
#Alice is eating 4...
#productNO: 5
#Tom is eating 5...
#Alice is eating 5...
#>>>
------------------------------------------------------------------------------
这篇关于python greenlet快速学习(tcy-)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!