python协程用法实例分析,python协实例分析
python协程用法实例分析,python协实例分析
本文实例讲述了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__slots_
- Python进程间通信用法实例,python进程用法实例
- python中引用与复制用法实例分析,python实例分析
- Python实现统计英文单词个数及字符串分割代码,
- Python中使用不同编码读写txt文件详解,
- Python import用法以及与from...import的区别,pythonfromimport
- Python实现给qq邮箱发送邮件的方法,python发送邮件
- Python下载懒人图库JavaScript特效,pythonjavascript
- Python实现统计单词出现的个数,python单词
- Python使用Scrapy爬取妹子图,pythonscrapy
评论关闭