1.13 协程

xiaoxiao2021-02-28  18

协程是输入一系列函数的参数,使用yield语句且表达式(yield)创建的。例如: def print_matches(matchtext): print "looking for",matchtext while True: line = (yield) #获得一行文本 if matchtext in line: print line    首先,调用该函数;其次,向前执行到第一条(yield)语句;最后,使用send()给它发送数据,例如: >>> matcher = print_matches("python") >>> matcher.next()  #向前执行到第一条(yield)语句 looking for python >>> matcher.send("Hello world") #因为函数的实参数是“python”,将执行同字符串是“python”的一行文本,因此这个命令无法执行 >>> matcher.send("python is cool") #执行 python is cool >>> matcher.send("yow!") #无法执行 >>> matcher.close() #匹配器函数调用结束
转载请注明原文地址: https://www.6miu.com/read-2649978.html

最新回复(0)