天天看點

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!      

繼續閱讀