Pit Example 1. Using the process to implement producer, consumer model
- Python support for the process is implemented through generator.
- Ref: 49924099
- Reference: Https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 001432090171191d05dae6e129940518d1d6cf6eeaaa969000
"""1. Implement the consumer producer model by using the co-process2. Python support for the process is implemented through generator3. If there is yield, it is generator."""defConsumer (): R= "' while True:# N is the value of send over # Yield is similar to a breakpoint and has two effects. # 1. Generate a value # 2. In this breakpoint, surrender control. N= yieldRif notN:return Print(' [CONSUMER] consuming%s... ' %N) r= ' OK 'defProduce (c):#start generator with NoneC.send (None) n= 0 whileN< 5: N=N+ 1 Print(' [PRODUCER] producing%s... ' %N#启动生成器 with a value that R receives yield-generated valuesR=C.send (N)Print(' [PRODUCER] Consumer return:%s' %R) C.close () c=Consumer () produce (c)
Operating system Os,python-co-process (Coroutine)