Day4 using yield for single-threaded

Source: Internet
Author: User

First, yield generator (yield)

Yield is used to end the while loop, and to maintain the state of the previous loop, the next call is executed directly from yield, executes the program after yield, and loops again, and yield can also be used to receive parameters, receive the parameters passed by send (), and assign to a variable.

Import time# imports the time module, which is used to give the producer a definition of the duration of the bun, such as when the bank processing is complete and tells the user the Def consumer (name): #消费者模块 Print ("ready to eat steamed buns! ....")     whileTrue: #循环, Dead loop, can be processed using yield, and remain in a certain state Baozi=yield#使用yield来接收send () pass the parameters, yield and send () using print ("Bun [%s] came, eaten by [%s]! "%(Baozi,name)) def producer (name): #定义生成这模型 C= Consumer ("A") #消费者来买包子 C2= Consumer ("B") c.__next__ () #调用consumer () function c2.__next__ () print ("Lao Zi began to make buns! ...")     forIinchRange1,Ten): Time.sleep (1) #生产者每秒钟做两个包子 Print ("made 2 buns! ") c.send (i) #把生产者生成的包子传递给yield c2.send (i) Producer ("Alex")

Above, we import time module, Time.sleep () Let the program stop for a certain time. The above code consumers eat steamed buns, producers to generate steamed buns, and the generation of steamed buns sent to consumers (send ()) to pass, the consumer yield to receive. This enables the producer to generate buns and deliver the buns to the consumer, enabling the association between two functions. The association between two functions is implemented without calling the function again.

The above program operation results are as follows:

Ready to eat steamed buns! ....
Ready to eat steamed buns! ....
Lao Zi began to make buns! ...
Made 2 buns!
Bun [1] came and was [A] eaten!
Bun [1] came and was [B] eaten!
Made 2 buns!
Bun [2] came and was [A] eaten!
Bun [2] came and was [B] eaten!
Made 2 buns!
Bun [3] came and was [A] eaten!
Bun [3] came and was [B] eaten!
Made 2 buns!
Bun [4] came and was [A] eaten!
Bun [4] came and was [B] eaten!
Made 2 buns!
Bun [5] came and was [A] eaten!
Bun [5] came and was [B] eaten!
Made 2 buns!
Bun [6] came and was [A] eaten!
Bun [6] came and was [B] eaten!
Made 2 buns!
Bun [7] came and was [A] eaten!
Bun [7] came and was [B] eaten!
Made 2 buns!
Bun [8] came and was [A] eaten!
Bun [8] came and was [B] eaten!
Made 2 buns!
Bun [9] came and was [A] eaten!
Bun [9] came and was [B] eaten!

ImportTime
#Import Timemodule, which is used to generate a defined time for the producer, such as when the bank processing is completed and the user is informed
defConsumer (name):
#Consumer Module
Print("ready to eat steamed buns! ....")
While True:
#loops, dead loops, can be usedyieldto handle and remain in a certain state.
Baozi =yield
#Useyieldto receiveSend ()passed over the parameters,yieldwith theSend ()combined use
Print("steamed Bun[%s]came, was[%s]Eat It! " % (Baozi,name))

defProducername):
#Define build this model
c = Consumer ("A")
#Consumers to buy buns
C2 = Consumer ("B")
C.__next__()
#calledconsumer ()function
C2.__next__()
Print("Lao Zi began to make buns! ...")
forIinchRange(1,Ten):
Time.sleep (1)
#producers make two buns per second
Print("did a2a bun! ")
C.send (i)
#The producer of the steamed buns delivered toyield
C2.send (i)

Producer"Alex")

Day4 using yield for single-threaded

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.