Home  >  Article  >  Backend Development  >  Detailed explanation of python generator coroutine operation with examples

Detailed explanation of python generator coroutine operation with examples

巴扎黑
巴扎黑Original
2017-09-05 11:11:201169browse

The following editor will bring you an example of python generator coroutine operation. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

1. Yield operation method

We define a generator as follows:


def put_on(name):
 print("Hi {}, 货物来了,准备搬到仓库!".format(name))
 while True:
  goods = yield
  print("货物[%s]已经被%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 into a generator through yield, no result will be returned when running the function directly. Because the function is already a generator at this time, we need to get the value through next(), and jump out of the function again when yield is encountered.


print(type(p))
 
#输出
<class &#39;generator&#39;>

We add the next() method:


def put_on(name):
 print("Hi {}, 货物来了,准备搬到仓库!".format(name))
 while True:
  goods = yield  #遇到yield中断
  print("货物[%s]已经被%s搬进仓库了。"%(goods,name)) #中断后运行部分
 
p = put_on("bigberg")
p.__next__()
 
#输出
Hi bigberg, 货物来了,准备搬到仓库!

At this time, the function interrupts at the place where goods = yield, When we call the next() function again, the function will only run the content after the interruption, that is, the part below yield in the above example.

We add another next():


def put_on(name):
 print("Hi {}, 货物来了,准备搬到仓库!".format(name))
 while True:
  goods = yield
  print("货物[%s]已经被%s搬进仓库了。"%(goods,name))
 
p = put_on("bigberg")
p.__next__()
p.__next__()
 
#输出
Hi bigberg, 货物来了,准备搬到仓库!
货物[None]已经被bigberg搬进仓库了。

We can run next() for the second time part of the content below yield, but it does not Pass a value to goods, so goods is None.

Summary:

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

yield Just retain the interrupt status of the function, and calling next() again will execute the part after yield

If yield does not return a value, it will return a None value

2. Send() passes value


##

def put_on(name):
 print("Hi {}, 货物来了,准备搬到仓库!".format(name))
 while True:
  goods = yield
  print("货物[%s]已经被%s搬进仓库了。"%(goods,name))
 
p = put_on("bigberg")
p.__next__()
p.send("瓜子")
 
#输出
Hi bigberg, 货物来了,准备搬到仓库!
货物[瓜子]已经被bigberg搬进仓库了。

Summary:

__next__() just calls this yield, which can also be said to wake up the yield, but it does not pass the value to the yield.

The send() method calls yield and can pass a value to yield

You must use __next__() before using the send() function, because it needs to be interrupted first. When it is called for the second time, Only then can the value be passed.


def put_on(name):
 print("Hi {}, 货物来了,准备搬到仓库!".format(name))
 while True:
  goods = yield
  print("货物[%s]已经被%s搬进仓库了。"%(goods,name))
 
p = put_on("bigberg")
p.__next__()
p.send("瓜子")
p.send("花生")
p.send("饼干")
p.send("牛奶")
 
#多次调用send()
Hi bigberg, 货物来了,准备搬到仓库!
货物[瓜子]已经被bigberg搬进仓库了。
货物[花生]已经被bigberg搬进仓库了。
货物[饼干]已经被bigberg搬进仓库了。
货物[牛奶]已经被bigberg搬进仓库了。

3. Single thread to achieve parallel effect (coroutine)


import time
 
 
def put_on(name):
 print("Hi {}, 货物来了,准备搬到仓库!".format(name))
 while True:
  goods = yield
  print("货物[%s]已经被%s搬进仓库了。"%(goods,name))
 
 
def transfer(name):
 p = put_on(&#39;A&#39;)
 p2 = put_on(&#39;B&#39;)
 p.__next__()
 p2.__next__()
 print("%s将货物送来了!"%name)
 for i in range(5):
  time.sleep(1)
  print("%s递过来两件货物"%name)
  p.send("瓜子")
  p2.send("花生")
 
transfer("bigberg")
 
#输出
Hi A, 货物来了,准备搬到仓库!
Hi B, 货物来了,准备搬到仓库!
bigberg将货物送来了!
bigberg递过来两件货物
货物[瓜子]已经被A搬进仓库了。
货物[花生]已经被B搬进仓库了。
bigberg递过来两件货物
货物[瓜子]已经被A搬进仓库了。
货物[花生]已经被B搬进仓库了。
bigberg递过来两件货物
货物[瓜子]已经被A搬进仓库了。
货物[花生]已经被B搬进仓库了。
bigberg递过来两件货物
货物[瓜子]已经被A搬进仓库了。
货物[花生]已经被B搬进仓库了。
bigberg递过来两件货物
货物[瓜子]已经被A搬进仓库了。
货物[花生]已经被B搬进仓库了。

The above is the detailed content of Detailed explanation of python generator coroutine operation with examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn