python协程用法实例分析
发布时间:2020-09-02 00:35:17 所属栏目: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程序设计有所帮助。 (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |