Producer and Consumer Model

Producer and consumer models, and this function is the process of executing concurrent operations in a single thread 
import
time def consume(name): print ( ' I am %s, I am going to start eating buns ' % name) while True: baozi = yield #The yield in the function is the generator function time.sleep(1 ) print ( ' %s ate the buns happily [%s] ' % (name,baozi)) def producer(): c1=consume('cat') c2=consume('dog') c1. __next__ () # As soon as you encounter yield, wait for the bun, and the program will not execute down there in yield # c2.__next__() # for i in range(10): time.sleep(1 ) c1.send( ' beef stuffing ' ) # c2.send('pork stuffing, steamed buns%s'%i) producer() #Concurrency is to process two functions at the same time, or multiple functions, concurrency in a single thread

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325234219&siteId=291194637