Example of Python generator co-op

Source: Internet
Author: User
The following small series for everyone to bring an example of the Python generator process operation. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

First, yield operation 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] has been moved to the warehouse by%s. "% (goods,name)) p = put_on (" Bigberg ") #输出G: \python\install\python.exe g:/python/untitled/study4/test/double.py Process finished with exit code 0

When we convert a function through yield into a generator, the function is directly run without the result returned. Since the function is already a generator, we are going to get the value through next () and jump out of the function again when we encounter yield.


Print (Type (p)) #输出 <class ' generator ' >

We 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  #遇到yield中断  print (" Cargo [%s] has been moved to the warehouse by%s. "% (goods,name)) #中断后运行部分 p = put_on (" Bigberg ") p.__next__ () #输出Hi Bigberg, the goods are coming, ready to move to the warehouse!

At this point the function breaks at goods = yield, and when we call the next () function again, the function will only run after the break, that is, the lower part of yield in the example above.

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] has been moved to the warehouse by%s. "% (goods,name)) p = put_on (" Bigberg ") p.__next__ () p.__next__ () #输出Hi Bigberg, the goods are coming, ready to move to the warehouse! The goods [None] have been Bigberg moved into the warehouse.

The second time we can run next () is part of yield, but it doesn't give goods value, so the goods are none.

Summary:

Convert a function to a generator through yield, you need to use the next () method to run

Yield simply preserves the interrupt state of the function, and another call to next () executes the part after yield

Yield if there is no return value, a null value of None is returned

Second, send () pass 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] has been moved to the warehouse by%s. "% (goods,name)) p = put_on (" Bigberg ") p.__next__ () p.send (" Melon seeds ") #输出Hi Bigberg, the goods come, ready to move to the warehouse! The goods [melon seeds] have been Bigberg moved into the warehouse.

Summary:

__NEXT__ () Just calls this yield, or it can be said to awaken yield, but does not give the yield a value.

Send () method call yield Yes, can pass value to yield

You must use __NEXT__ () before using the Send () function, because you want to break first, and you can pass a value when you call it a second 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] has been moved to the warehouse by%s. "% (goods,name)) p = put_on (" Bigberg ") p.__next__ () p.send (" Melon Seeds ") p.send (" Peanuts ") p.send (" Biscuit ") p.send (" Milk ") #多次调用send () Hi Bigberg, the goods are coming, ready to move to the warehouse! The goods [melon seeds] have been Bigberg moved into the warehouse. The goods [peanuts] have been bigberg into the warehouse. The goods [biscuits] have been bigberg into the warehouse. The goods [milk] have been bigberg into the warehouse.

Third, single-threaded implementation of parallel effects (co-process)


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 (" cargo [%s] has been moved to 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 over two pieces of goods"%name) p.send ("Melon Seeds") p2.send ("Peanuts") Transfer ("Bigberg") #输出Hi A , the goods are coming, ready to move to the warehouse! Hi B, the goods are coming, ready to move to the warehouse! Bigberg sent the goods to!bigberg handed over two pieces of cargo [melon seeds] have been moved into the warehouse. The goods [peanuts] have been moved into the warehouse by B. Bigberg handed over two pieces of cargo [melon seeds] have been moved into the warehouse. The goods [peanuts] have been moved into the warehouse by B. Bigberg handed over two pieces of cargo [melon seeds] have been moved into the warehouse. The goods [peanuts] have been moved into the warehouse by B. Bigberg handed over two pieces of cargo [melon seeds] have been moved into the warehouse. The goods [peanuts] have been moved into the warehouse by B. Bigberg handed over two pieces of cargo [melon seeds] have been moved into the warehouse. The goods [peanuts] have been moved into the warehouse by B. 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.