基于python生成器封裝的協(xié)程類
自從python2.2提供了yield關(guān)鍵字之后,python的生成器的很大一部分用途就是可以用來構(gòu)建協(xié)同程序,能夠?qū)⒑瘮?shù)掛起返回中間值并能從上次離開的地方繼續(xù)執(zhí)行。python2.5的時(shí)候,這種生成器更加接近完全的協(xié)程,因?yàn)樘峁┝藢⒅岛彤惓鬟f回到一個(gè)繼續(xù)執(zhí)行的函數(shù)中,當(dāng)?shù)却善鞯臅r(shí)候,生成器能返回控制。
python提供的生成器設(shè)施:
- yield:能夠?qū)⒆约簰炱穑⑻峁┮粋€(gè)返回值給等待方
- send:?jiǎn)酒鹨粋€(gè)被掛起的生成器,并能夠傳遞一個(gè)參數(shù),可以在生成器中拋出異常
- next:本質(zhì)上相當(dāng)于send(None),對(duì)每個(gè)生成器的第一次調(diào)用必須不能傳遞參數(shù)
- close:主動(dòng)退出一個(gè)生成器
python封裝
雖然python3提供了asyncio這樣的異步IO庫,而且也有g(shù)reenlet等其他協(xié)程庫,但目前的需求并不是實(shí)際的網(wǎng)絡(luò)IO并發(fā)操作,而是需要模擬狀態(tài)機(jī)的運(yùn)行,因此使用協(xié)程可以很方便的模擬,并加入認(rèn)為的控制,下面是封裝的一個(gè)python類。
class Coroutine(object):
""" Base class of the general coroutine object """
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
while True:
self.args = yield self.ret
if not self.started:
self.started = True
continue
else:
self.state = Coroutine.STATE_RUNNING
self.ret = self.run(self.args)
if self.state == Coroutine.STATE_CLOSING:
break
self.state = Coroutine.STATE_WAITING
def start(self):
""" Start the generator """
if self.routine is None:
raise RuntimeError('NO task to start running!')
self.started = True
self.routine.next()
def finish(self):
""" Finish the execution of this routine """
self.state = Coroutine.STATE_CLOSING
self.routine.close()
def run(self, args):
""" The runing method to be executed every once time"""
raise NotImplementedError
def execute(self, arg_obj):
""" Awake this routine to execute once time """
return self.routine.send(arg_obj)
基于上述封裝,下面實(shí)現(xiàn)了一個(gè)協(xié)同的生產(chǎn)者消費(fèi)者示例:
class ProducerCoroutine(Coroutine):
""" The Producer concrete coroutine """
def __init__(self, cnsmr):
if not isinstance(cnsmr, Coroutine):
raise RuntimeError('Consumer is not a Coroutine object')
self.consumer = cnsmr
self.consumer.start()
super(ProducerCoroutine, self).__init__()
def run(self, args):
print 'produce ', args
ret = self.consumer.execute(args)
print 'consumer return:', ret
def __call__(self, args):
""" Custom method for the specific logic """
self.start()
while len(args) > 0:
p = args.pop()
self.execute(p)
self.finish()
class ConsumerCoroutine(Coroutine):
""" The Consumer concrete coroutine """
def __init__(self):
super(ConsumerCoroutine, self).__init__()
def run(self, args):
print 'consumer get args: ', args
return 'hahaha' + repr(args)
運(yùn)行結(jié)果如下:
produce 4 consumer get args: 4 consumer return: hahaha4 produce 3 consumer get args: 3 consumer return: hahaha3 produce 2 consumer get args: 2 consumer return: hahaha2 produce 1 consumer get args: 1 consumer return: hahaha1 produce 0 consumer get args: 0 consumer return: hahaha0
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python中torch可以成功引用但無法訪問屬性的解決辦法
這篇文章給大家介紹了我們?cè)趐ython中運(yùn)行程序時(shí)遇到一個(gè)奇怪的報(bào)錯(cuò),torch可以成功引用但無法訪問屬性,這是比較奇怪的一件事,因?yàn)閠orch肯定是可以訪問Tensor,所以本文給大家介紹了torch可以成功引用但無法訪問屬性的解決辦法,需要的朋友可以參考下2024-01-01
python實(shí)現(xiàn)實(shí)時(shí)監(jiān)控文件的方法
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)實(shí)時(shí)監(jiān)控文件的3種方法,感興趣的小伙伴們可以參考一下2016-08-08
Python求最小公倍數(shù)與最大公約數(shù)代碼示例與解題思路
這篇文章主要給大家介紹了Python求最小公倍數(shù)與最大公約數(shù)代碼示例與解題思路的相關(guān)資料,包括迭代法、使用math模塊的lcm和gcd函數(shù)以及輾轉(zhuǎn)相除法,需要的朋友可以參考下2024-11-11
Django + Uwsgi + Nginx 實(shí)現(xiàn)生產(chǎn)環(huán)境部署的方法
Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比較常見的一種方式。這篇文章主要介紹了Django + Uwsgi + Nginx 實(shí)現(xiàn)生產(chǎn)環(huán)境部署,感興趣的小伙伴們可以參考一下2018-06-06
django如何根據(jù)現(xiàn)有數(shù)據(jù)庫表生成model詳解
這篇文章主要給大家介紹了關(guān)于django如何根據(jù)現(xiàn)有數(shù)據(jù)庫表生成model的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-08-08
Python 實(shí)現(xiàn)Image和Ndarray互相轉(zhuǎn)換
今天小編就為大家分享一篇Python 實(shí)現(xiàn)Image和Ndarray互相轉(zhuǎn)換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
在CentOS 7中使用Python 3執(zhí)行系統(tǒng)命令的詳細(xì)教程
使用os.system()這個(gè)方法簡(jiǎn)單直接,但它不返回命令的輸出,只返回命令的退出狀態(tài),如果你只需要知道命令是否成功執(zhí)行,這個(gè)方法就足夠了,這篇文章主要介紹了在CentOS 7中使用Python 3執(zhí)行系統(tǒng)命令的詳細(xì)教程,需要的朋友可以參考下2024-02-02
Python OpenCV實(shí)現(xiàn)鼠標(biāo)畫框效果
這篇文章主要為大家詳細(xì)介紹了Python OpenCV實(shí)現(xiàn)鼠標(biāo)畫框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08

