我是靠谱客的博主 含糊曲奇,这篇文章主要介绍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协程用法实例分析的全部内容,更多相关python协程用法实例分析内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(223)

评论列表共有 0 条评论

立即
投稿
返回
顶部