基于python生成器封装的协程类


Posted in Python onMarch 20, 2019

自从python2.2提供了yield关键字之后,python的生成器的很大一部分用途就是可以用来构建协同程序,能够将函数挂起返回中间值并能从上次离开的地方继续执行。python2.5的时候,这种生成器更加接近完全的协程,因为提供了将值和异常传递回到一个继续执行的函数中,当等待生成器的时候,生成器能返回控制。

python提供的生成器设施:

  • yield:能够将自己挂起,并提供一个返回值给等待方
  • send:唤起一个被挂起的生成器,并能够传递一个参数,可以在生成器中抛出异常
  • next:本质上相当于send(None),对每个生成器的第一次调用必须不能传递参数
  • close:主动退出一个生成器

python封装

虽然python3提供了asyncio这样的异步IO库,而且也有greenlet等其他协程库,但目前的需求并不是实际的网络IO并发操作,而是需要模拟状态机的运行,因此使用协程可以很方便的模拟,并加入认为的控制,下面是封装的一个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)

基于上述封装,下面实现了一个协同的生产者消费者示例:

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)

运行结果如下:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python操作数据库之sqlite3打开数据库、删除、修改示例
Mar 13 Python
Python中数组,列表:冒号的灵活用法介绍(np数组,列表倒序)
Apr 18 Python
Python常用的json标准库
Feb 19 Python
tensorflow如何继续训练之前保存的模型实例
Jan 21 Python
Python3读写Excel文件(使用xlrd,xlsxwriter,openpyxl3种方式读写实例与优劣)
Feb 13 Python
pycharm运行程序时看不到任何结果显示的解决
Feb 21 Python
Python使用GitPython操作Git版本库的方法
Feb 29 Python
django中related_name的用法说明
May 20 Python
学习python需要有编程基础吗
Jun 02 Python
python logging模块的使用
Sep 07 Python
Python第三方库安装缓慢的解决方法
Feb 06 Python
pycharm2021激活码使用教程(永久激活亲测可用)
Mar 30 Python
python实现栅栏加解密 支持密钥加密
Mar 20 #Python
python实现Virginia无密钥解密
Mar 20 #Python
python实现维吉尼亚加密法
Mar 20 #Python
Python multiprocess pool模块报错pickling error问题解决方法分析
Mar 20 #Python
python实现对输入的密文加密
Mar 20 #Python
python实现字符串加密成纯数字
Mar 19 #Python
python实现简单加密解密机制
Mar 19 #Python
You might like
php缓冲 output_buffering的使用详解
2013/06/13 PHP
通过PHP自带的服务器来查看正则匹配结果的方法
2015/12/24 PHP
Laravel 集成微信用户登录和绑定的实现
2019/12/27 PHP
JS 学习笔记 防止发生命名冲突
2009/07/30 Javascript
JavaScript中的apply()方法和call()方法使用介绍
2012/07/25 Javascript
nullJavascript中创建对象的五种方法实例
2013/05/07 Javascript
JS获取计算机mac地址以及IP的实现方法
2014/01/08 Javascript
javascript函数重载解决方案分享
2014/02/19 Javascript
jquery选择器原理介绍($()使用方法)
2014/03/25 Javascript
jQuery的animate函数实现图文切换动画效果
2015/05/03 Javascript
Ionic快速安装教程
2016/06/03 Javascript
JavaScript实现移动端滑动选择日期功能
2016/06/21 Javascript
关于JS中二维数组的声明方法
2016/09/24 Javascript
vue.js指令v-for使用及索引获取
2016/11/03 Javascript
vue计算属性及使用详解
2018/04/02 Javascript
微信小程序实现两边小中间大的轮播效果的示例代码
2018/12/07 Javascript
Python 多线程搜索txt文件的内容,并写入搜到的内容(Lock)方法
2019/08/23 Python
python GUI库图形界面开发之PyQt5线程类QThread详细使用方法
2020/02/26 Python
django 模型中的计算字段实例
2020/05/19 Python
Python3爬虫发送请求的知识点实例
2020/07/30 Python
容易被忽略的Python内置类型
2020/09/03 Python
python 获取字典特定值对应的键的实现
2020/09/29 Python
Html5游戏开发之乒乓Ping Pong游戏示例(三)
2013/01/21 HTML / CSS
html5读取本地文件示例代码
2014/04/22 HTML / CSS
zooplus波兰:在线宠物店
2019/07/21 全球购物
淘宝客服自我总结鉴定
2014/01/25 职场文书
党校培训自我鉴定
2014/02/01 职场文书
工程专业求职自荐书范文
2014/02/08 职场文书
学校安全责任书范本
2014/07/23 职场文书
2014感恩节演讲稿大全
2014/10/11 职场文书
整改落实自查报告
2014/11/05 职场文书
研究生毕业论文导师评语
2014/12/31 职场文书
《索溪峪的野》教学反思
2016/02/19 职场文书
教您怎么制定西餐厅运营方案 ?
2019/07/05 职场文书
聊聊pytorch测试的时候为何要加上model.eval()
2021/05/23 Python
golang语言指针操作
2022/04/14 Golang