This article mainly introduces the usage of the python coroutine. The example analyzes the concept, functions, and usage of the Protocol in Python. If you need it, refer to the example in this article to describe the usage of 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.