溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python中如何使用生成器封裝協(xié)程類

發(fā)布時(shí)間:2021-06-24 10:52:48 來源:億速云 閱讀:155 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)python中如何使用生成器封裝協(xié)程類,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

自從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:喚起一個(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

關(guān)于“python中如何使用生成器封裝協(xié)程類”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI