Python coroutine usage Example 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.
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
Def print_matchs (matchtext ):
Print "looking for", matchtext
While True:
Line = (yield)
# Use the yield statement and create a coroutine in the form of an expression (yield)
If matchtext in line:
Print line
>>> Matcher = print_matchs ('python ')
>>> Matcher. next ()
Looking for python
>>> Matcher. send ('Hello python') # check the generator. The 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.