Python generator coroutine computing instance, python Generator
I. yield Running Mode
We define a generator as follows:
Def put_on (name): print ("Hi {}, the goods are coming, ready to move to the warehouse! ". Format (name) while True: goods = yield print (" goods [% s] have been moved into the warehouse by % s. "% (Goods, name) p = put_on (" bigberg ") # output G: \ python \ install \ python.exe G:/python/untitled/study4/test/double. py Process finished with exit code 0
When we convert a function to a generator through yield, directly running the function will not return the result. Because the function is already a generator, we need to get the value through next () and jump out of the function again when yield is encountered.
Print (type (p) # output <class 'generator'>
Let's add the next () method:
Def put_on (name): print ("Hi {}, the goods are coming, ready to move to the warehouse! ". Format (name) while True: goods = yield # The yield interrupt print (" goods [% s] has been moved into the warehouse by % s. "% (Goods, name) # Run Part p = put_on (" bigberg ") p. _ next _ () # output Hi bigberg. The goods are ready to be moved to the warehouse!
At this time, the function is interrupted at the place where goods = yield. When we call the next () function again, the function only runs the content after the interruption, that is, the part below yield in the above example.
Let's add another next ():
Def put_on (name): print ("Hi {}, the goods are coming, ready to move to the warehouse! ". Format (name) while True: goods = yield print (" goods [% s] have been moved into the warehouse by % s. "% (Goods, name) p = put_on (" bigberg ") p. _ next _ () p. _ next _ () # output Hi bigberg. The goods are ready to be moved to the warehouse! The goods [None] have been moved into the warehouse by bigberg.
We can run part of yield for the second time next (), but it does not pass the value to goods, so the goods are None.
Summary:
To convert a function to a generator using yield, you must use the next () method to run the function.
Yield only retains the interrupt state of the function. If you call next () Again, the part after yield is executed.
If yield does not return a value, a null value of None is returned.
2. send () to pass the value
Def put_on (name): print ("Hi {}, the goods are coming, ready to move to the warehouse! ". Format (name) while True: goods = yield print (" goods [% s] have been moved into the warehouse by % s. "% (Goods, name) p = put_on (" bigberg ") p. _ next _ () p. send ("melon seeds") # output Hi bigberg, goods come, ready to move to the warehouse! The goods [melon seeds] have been moved into the warehouse by bigberg.
Summary:
_ Next _ () only calls this yield. It can also be called to wake up yield, but it does not pass a value to yield.
Yes When the send () method calls yield. It can pass a value to yield.
Before using the send () function, you must use _ next _ (), because the value must be interrupted before the second call.
Def put_on (name): print ("Hi {}, the goods are coming, ready to move to the warehouse! ". Format (name) while True: goods = yield print (" goods [% s] have been moved into the warehouse by % s. "% (Goods, name) p = put_on (" bigberg ") p. _ next _ () p. send ("melon seeds") p. send ("Peanut") p. send ("biscuit") p. send ("milk") # Call send () Hi bigberg multiple times. The goods are ready to be moved to the warehouse! The goods [melon seeds] have been moved into the warehouse by bigberg. The cargo [peanut] has been moved into the warehouse by bigberg. The cargo [biscuit] has been moved into the warehouse by bigberg. The goods [milk] have been moved into the warehouse by bigberg.
3. Parallel effect of a single thread (coroutine)
Import time def put_on (name): print ("Hi {}, the goods are coming, ready to move to the warehouse! ". Format (name) while True: goods = yield print (" goods [% s] have been moved into the warehouse by % s. "% (Goods, name) def transfer (name): p = put_on ('A') p2 = put_on ('B') p. _ next _ () p2. _ next _ () print ("% s sent the goods! "% Name) for I in range (5): time. sleep (1) print ("% s handed two goods" % name) p. send ("melon seeds") p2.send ("Peanut") transfer ("bigberg") # output Hi A, the goods are coming, ready to move to the warehouse! Hi B, the goods are coming. We are going to move to the warehouse! Bigberg sent the goods! Bigberg handed two goods [melon seeds] And A moved them into the warehouse. The cargo [peanut] has been moved into the warehouse by B. Bigberg handed two goods [melon seeds] And A moved them into the warehouse. The cargo [peanut] has been moved into the warehouse by B. Bigberg handed two goods [melon seeds] And A moved them into the warehouse. The cargo [peanut] has been moved into the warehouse by B. Bigberg handed two goods [melon seeds] And A moved them into the warehouse. The cargo [peanut] has been moved into the warehouse by B. Bigberg handed two goods [melon seeds] And A moved them into the warehouse. The cargo [peanut] has been moved into the warehouse by B.
The above python generator coroutine computation instance is all the content that I have shared with you. I hope to give you a reference and support for the help house.