基于python生成器封装的协程类
自从python2.2提供了yield关键字之后,python的生成器的很大一部分用途就是可以用来构建协同程序,能够将函数挂起返回中间值并能从上次离开的地方继续执行。python2.5的时候,这种生成器更加接近完全的协程,因为提供了将值和异常传递回到一个继续执行的函数中,当等待生成器的时候,生成器能返回控制。
python提供的生成器设施:
- yield:能够将自己挂起,并提供一个返回值给等待方
- send:唤起一个被挂起的生成器,并能够传递一个参数,可以在生成器中抛出异常
- next:本质上相当于send(None),对每个生成器的第一次调用必须不能传递参数
- close:主动退出一个生成器
python封装
虽然python3提供了asyncio这样的异步IO库,而且也有greenlet等其他协程库,但目前的需求并不是实际的网络IO并发操作,而是需要模拟状态机的运行,因此使用协程可以很方便的模拟,并加入认为的控制,下面是封装的一个python类。
classCoroutine(object):
"""Baseclassofthegeneralcoroutineobject"""
STATE_RUNNING=0
STATE_WAITING=1
STATE_CLOSING=2
def__init__(self):
self.state=Coroutine.STATE_WAITING
self.started=False
self.args=None
self.routine=self._co()
def_co(self):
self.ret=None
whileTrue:
self.args=yieldself.ret
ifnotself.started:
self.started=True
continue
else:
self.state=Coroutine.STATE_RUNNING
self.ret=self.run(self.args)
ifself.state==Coroutine.STATE_CLOSING:
break
self.state=Coroutine.STATE_WAITING
defstart(self):
"""Startthegenerator"""
ifself.routineisNone:
raiseRuntimeError('NOtasktostartrunning!')
self.started=True
self.routine.next()
deffinish(self):
"""Finishtheexecutionofthisroutine"""
self.state=Coroutine.STATE_CLOSING
self.routine.close()
defrun(self,args):
"""Theruningmethodtobeexecutedeveryoncetime"""
raiseNotImplementedError
defexecute(self,arg_obj):
"""Awakethisroutinetoexecuteoncetime"""
returnself.routine.send(arg_obj)
基于上述封装,下面实现了一个协同的生产者消费者示例:
classProducerCoroutine(Coroutine):
"""TheProducerconcretecoroutine"""
def__init__(self,cnsmr):
ifnotisinstance(cnsmr,Coroutine):
raiseRuntimeError('ConsumerisnotaCoroutineobject')
self.consumer=cnsmr
self.consumer.start()
super(ProducerCoroutine,self).__init__()
defrun(self,args):
print'produce',args
ret=self.consumer.execute(args)
print'consumerreturn:',ret
def__call__(self,args):
"""Custommethodforthespecificlogic"""
self.start()
whilelen(args)>0:
p=args.pop()
self.execute(p)
self.finish()
classConsumerCoroutine(Coroutine):
"""TheConsumerconcretecoroutine"""
def__init__(self):
super(ConsumerCoroutine,self).__init__()
defrun(self,args):
print'consumergetargs:',args
return'hahaha'+repr(args)
运行结果如下:
produce4 consumergetargs:4 consumerreturn:hahaha4 produce3 consumergetargs:3 consumerreturn:hahaha3 produce2 consumergetargs:2 consumerreturn:hahaha2 produce1 consumergetargs:1 consumerreturn:hahaha1 produce0 consumergetargs:0 consumerreturn:hahaha0
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。