python协程用法实例分析
2015-06-04 00:00
1031 查看
本文实例讲述了python协程用法。分享给大家供大家参考。具体如下:
把函数编写为一个任务,从而能处理发送给他的一系列输入,这种函数称为协程
def print_matchs(matchtext): print "looking for",matchtext while True: line = (yield) #用 yield语句并以表达式(yield)的形式创建协程 if matchtext in line: print line >>> matcher = print_matchs('python') >>> matcher.next() looking for python >>> matcher.send('hello python')#看生成器那片,关于send()跟next()的区别 hello python >>> matcher.send('test') >>> matcher.send('python is cool') python is cool >>>matcher.close()
希望本文所述对大家的Python程序设计有所帮助。
相关文章推荐
- python中__slots__用法实例
- Python进程间通信用法实例
- python中引用与复制用法实例分析
- Python3写的一个下载妹子图的工具,Very Nice
- Python 深入理解yield
- python 找某个文件夹下第多少个文件
- 【python学习笔记二】基本数据类型
- Python学习(七) 流程控制if语句
- Windows下python环境安装scrapy
- 用Python开始机器学习(2:决策树分类算法)
- python中使用libsvm
- Python自学笔记系列一——认识python
- Python文档生成工具pydoc
- Python中 *args 和 **kwargs的用法
- 终于不再是hello world
- python2.7爬虫学习笔记(一)---Urllib库的使用
- Dive into Python(深入Python) 读书笔记
- Python核心编程 读书笔记
- A byte of Python(Python简明教程) 读书笔记
- HBase 单台服务器启动多个 thrift 服务