Python coroutine usage instance analysis and python coroutine instance analysis
This example describes how to use the python coroutine. Share it with you for your reference. The details are as follows:
Write a function as a task to get a haircut and give it a series of input. This function is called a coroutine.
Def print_matchs (matchtext): print "looking for", matchtext while True: line = (yield) # create a coroutine if matchtext in line using the yield statement and the expression (yield: print line >>> matcher = print_matchs ('python') >>> matcher. next () looking for python >>> matcher. send ('Hello python') # check the generator. Difference Between send () and next (): hello python >>> matcher. send ('test') >>> matcher. send ('python is cool ') python is cool >>> matcher. close ()
I hope this article will help you with Python programming.