python 协程

 

def grep(pattern):
        print("Searching for", pattern)
        while True:
                line = (yield)
                if pattern in line:
                        print(line)

search = grep('coroutine')
next(search)

search.send("I love you")
search.send("Don't you love me?")
search.send("I love coroutine instead!")
search.close()

输出:

Searching for coroutine
I love coroutine instead!

 

posted @ 2018-11-09 22:45  anobscureretreat  阅读(155)  评论(0编辑  收藏  举报